forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciidoc.go
52 lines (44 loc) · 1.55 KB
/
asciidoc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"regexp"
"strings"
"github.com/bytesparadise/libasciidoc"
"github.com/bytesparadise/libasciidoc/pkg/configuration"
)
func parseWikilinks(asciidoc string) string {
// Define the regex pattern to match the wiki links
pattern := regexp.MustCompile(`\[\[([^\|\]]+)(?:\|([^\]]+))?\]\]`)
// Define the replacement function
replacement := func(match string) string {
submatches := pattern.FindStringSubmatch(match)
target := strings.TrimSpace(submatches[1])
display := strings.TrimSpace(submatches[2])
if display == "" {
display = target
}
targetFormatted := strings.ToLower(strings.ReplaceAll(target, " ", "-"))
return fmt.Sprintf("[.bg-lavender.dark:prose:text-neutral-50.dark:text-neutral-50.dark:bg-garnet.px-1]#%s# [.wikilinks]#(link:https://wikistr.com/%s[Wikistr], link:https://wikifreedia.xyz/%s[Wikifreedia])#", display, targetFormatted, targetFormatted)
}
// Use regex ReplaceAllStringFunc with the replacement function
transformedText := pattern.ReplaceAllStringFunc(asciidoc, replacement)
return transformedText
}
func asciidocToHTML(asciidoc string) string {
//Parsing wikilinks
asciidoc = parseWikilinks(asciidoc)
// Rendering
input := strings.NewReader(asciidoc)
var output = &strings.Builder{}
config := configuration.NewConfiguration(
[]configuration.Setting{
configuration.WithFilename("test.adoc"),
configuration.WithBackEnd("html5"),
}...,
)
_, err := libasciidoc.Convert(input, output, config)
if err != nil {
log.Printf("Error converting AsciiDoc: %v", err)
}
return output.String()
}