Add Markdown diagrams and render hooks for code blocks

You can now create custom hook templates for code blocks, either one for all (`render-codeblock.html`) or for a given code language (e.g. `render-codeblock-go.html`).

We also used this new hook to add support for diagrams in Hugo:

* Goat (Go ASCII Tool) is built-in and enabled by default; just create a fenced code block with the language `goat` and start draw your Ascii diagrams.
* Another popular alternative for diagrams in Markdown, Mermaid (supported by GitHub), can also be implemented with a simple template. See the Hugo documentation for more information.

Updates #7765
Closes #9538
Fixes #9553
Fixes #8520
Fixes #6702
Fixes #9558
This commit is contained in:
Bjørn Erik Pedersen
2022-02-17 13:04:00 +01:00
parent 2c20f5bc00
commit 08fdca9d93
73 changed files with 1887 additions and 1986 deletions

View File

@@ -11,34 +11,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package os
package os_test
import (
"path/filepath"
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/hugolib"
"github.com/gohugoio/hugo/tpl/os"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
)
func TestReadFile(t *testing.T) {
t.Parallel()
c := qt.New(t)
workingDir := "/home/hugo"
b := newFileTestBuilder(t).Build()
v := config.New()
v.Set("workingDir", workingDir)
// helpers.PrintFs(b.H.PathSpec.BaseFs.Work, "", _os.Stdout)
// f := newTestFuncsterWithViper(v)
ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
afero.WriteFile(ns.deps.Fs.Source, filepath.Join("/home", "f2.txt"), []byte("f2-content"), 0755)
ns := os.New(b.H.Deps)
for _, test := range []struct {
filename string
@@ -53,13 +45,13 @@ func TestReadFile(t *testing.T) {
result, err := ns.ReadFile(test.filename)
if b, ok := test.expect.(bool); ok && !b {
c.Assert(err, qt.Not(qt.IsNil))
if bb, ok := test.expect.(bool); ok && !bb {
b.Assert(err, qt.Not(qt.IsNil))
continue
}
c.Assert(err, qt.IsNil)
c.Assert(result, qt.Equals, test.expect)
b.Assert(err, qt.IsNil)
b.Assert(result, qt.Equals, test.expect)
}
}
@@ -67,15 +59,8 @@ func TestFileExists(t *testing.T) {
t.Parallel()
c := qt.New(t)
workingDir := "/home/hugo"
v := config.New()
v.Set("workingDir", workingDir)
ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
afero.WriteFile(ns.deps.Fs.Source, filepath.Join("/home", "f2.txt"), []byte("f2-content"), 0755)
b := newFileTestBuilder(t).Build()
ns := os.New(b.H.Deps)
for _, test := range []struct {
filename string
@@ -101,15 +86,8 @@ func TestFileExists(t *testing.T) {
func TestStat(t *testing.T) {
t.Parallel()
c := qt.New(t)
workingDir := "/home/hugo"
v := config.New()
v.Set("workingDir", workingDir)
ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
b := newFileTestBuilder(t).Build()
ns := os.New(b.H.Deps)
for _, test := range []struct {
filename string
@@ -123,11 +101,28 @@ func TestStat(t *testing.T) {
result, err := ns.Stat(test.filename)
if test.expect == nil {
c.Assert(err, qt.Not(qt.IsNil))
b.Assert(err, qt.Not(qt.IsNil))
continue
}
c.Assert(err, qt.IsNil)
c.Assert(result.Size(), qt.Equals, test.expect)
b.Assert(err, qt.IsNil)
b.Assert(result.Size(), qt.Equals, test.expect)
}
}
func newFileTestBuilder(t *testing.T) *hugolib.IntegrationTestBuilder {
files := `
-- f/f1.txt --
f1-content
-- home/f2.txt --
f2-content
`
return hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
WorkingDir: "/mywork",
},
)
}