sync: Use goldmark instead of blackfriday
This commit is contained in:
parent
72add55723
commit
f690a4e1c8
5 changed files with 111 additions and 111 deletions
|
@ -2,65 +2,99 @@ package sync
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/base32"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/russross/blackfriday/v2"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/renderer/html"
|
||||
"github.com/yuin/goldmark/text"
|
||||
"github.com/yuin/goldmark/util"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
func ProcessMarkdown(i Importer, input string, rootDir string) (output string, err error) {
|
||||
// Define the path where save linked files
|
||||
hash := blake2b.Sum512([]byte(rootDir))
|
||||
absPath := "$FILES$/" + strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:]))
|
||||
|
||||
// Process md
|
||||
output = string(blackfriday.Run(
|
||||
[]byte(input),
|
||||
blackfriday.WithRenderer(blackfriday.NewHTMLRenderer(
|
||||
blackfriday.HTMLRendererParameters{
|
||||
AbsolutePrefix: absPath,
|
||||
Flags: blackfriday.CommonHTMLFlags,
|
||||
},
|
||||
)),
|
||||
))
|
||||
markdown := goldmark.New(
|
||||
goldmark.WithExtensions(extension.DefinitionList),
|
||||
goldmark.WithExtensions(extension.Linkify),
|
||||
goldmark.WithExtensions(extension.Strikethrough),
|
||||
goldmark.WithExtensions(extension.Table),
|
||||
goldmark.WithExtensions(extension.Typographer),
|
||||
goldmark.WithParserOptions(
|
||||
parser.WithASTTransformers(
|
||||
util.Prioritized(NewImageImporterTransformer(i, rootDir, hash), 200),
|
||||
),
|
||||
),
|
||||
goldmark.WithRendererOptions(
|
||||
html.WithHardWraps(),
|
||||
),
|
||||
)
|
||||
|
||||
// Import files
|
||||
var re *regexp.Regexp
|
||||
re, err = regexp.Compile(strings.Replace(absPath, "$", "\\$", -1) + "/[^\"]+")
|
||||
if err != nil {
|
||||
var buf bytes.Buffer
|
||||
context := parser.NewContext()
|
||||
if err = markdown.Convert([]byte(input), &buf, parser.WithContext(context)); err != nil {
|
||||
return
|
||||
}
|
||||
files := re.FindAllString(output, -1)
|
||||
|
||||
for _, filePath := range files {
|
||||
iPath := strings.TrimPrefix(filePath, absPath)
|
||||
dPath := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:])), iPath)
|
||||
|
||||
if err = os.MkdirAll(path.Dir(dPath), 0755); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var fdto *os.File
|
||||
if fdto, err = os.Create(dPath); err != nil {
|
||||
return
|
||||
} else {
|
||||
defer fdto.Close()
|
||||
writer := bufio.NewWriter(fdto)
|
||||
if err = getFile(i, rootDir+iPath, writer); err != nil {
|
||||
os.Remove(dPath)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
output = string(buf.Bytes())
|
||||
|
||||
// Trim output
|
||||
output = strings.TrimSpace(output)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type imageImporterTransformer struct {
|
||||
importer Importer
|
||||
rootDir string
|
||||
hash [blake2b.Size]byte
|
||||
absPath string
|
||||
}
|
||||
|
||||
func NewImageImporterTransformer(i Importer, rootDir string, hash [blake2b.Size]byte) parser.ASTTransformer {
|
||||
absPath := "$FILES$/" + strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:]))
|
||||
return &imageImporterTransformer{i, rootDir, hash, absPath}
|
||||
}
|
||||
|
||||
func (t *imageImporterTransformer) Transform(doc *ast.Document, reader text.Reader, pc parser.Context) {
|
||||
ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
|
||||
if !enter {
|
||||
return ast.WalkContinue, nil
|
||||
}
|
||||
|
||||
switch child := node.(type) {
|
||||
case *ast.Image:
|
||||
iPath := string(child.Destination)
|
||||
dPath := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(t.hash[:])), iPath)
|
||||
child.Destination = []byte(path.Join(t.absPath, string(child.Destination)))
|
||||
|
||||
if err := os.MkdirAll(path.Dir(dPath), 0755); err != nil {
|
||||
return ast.WalkStop, err
|
||||
}
|
||||
|
||||
if fdto, err := os.Create(dPath); err != nil {
|
||||
return ast.WalkStop, err
|
||||
} else {
|
||||
defer fdto.Close()
|
||||
writer := bufio.NewWriter(fdto)
|
||||
if err := getFile(t.importer, path.Join(t.rootDir, iPath), writer); err != nil {
|
||||
os.Remove(dPath)
|
||||
return ast.WalkStop, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ast.WalkContinue, nil
|
||||
})
|
||||
}
|
||||
|
|
Reference in a new issue