Repository navigation
Expand file tree
/
Copy pathrender.go
More file actions
411 lines (378 loc) · 10.9 KB
/
Copy pathrender.go
File metadata and controls
411 lines (378 loc) · 10.9 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"bytes"
"fmt"
"path"
"regexp"
"sort"
"strconv"
"strings"
"unicode"
"github.com/alecthomas/chroma/v2"
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
extast "github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
// ---------- 代码高亮(chroma,和 Hugo / Gitea 同款) ----------
var codeFormatter = chromahtml.New(
chromahtml.WithClasses(true),
chromahtml.WithLineNumbers(true),
chromahtml.LineNumbersInTable(true), // 表格形式,复制代码时不会带上行号
chromahtml.WithLinkableLineNumbers(true, "L"),
chromahtml.TabWidth(4),
)
// 一些 chroma 认不出来的常见文件名
var lexerAlias = map[string]string{
"dockerfile": "docker",
"containerfile": "docker",
"makefile": "make",
"jenkinsfile": "groovy",
"vagrantfile": "ruby",
".bashrc": "bash",
".zshrc": "bash",
".profile": "bash",
".bash_profile": "bash",
".gitignore": "plaintext",
".env": "bash",
"go.mod": "go",
"go.sum": "plaintext",
"kubeconfig": "yaml",
"requirements.txt": "plaintext",
}
func pickLexer(name, src string) chroma.Lexer {
lower := strings.ToLower(name)
if alias, ok := lexerAlias[lower]; ok {
if l := lexers.Get(alias); l != nil {
return l
}
}
if strings.HasPrefix(lower, "dockerfile") {
return lexers.Get("docker")
}
if l := lexers.Match(name); l != nil {
return l
}
switch strings.ToLower(path.Ext(name)) {
case ".conf", ".cnf", ".cfg":
return lexers.Get("ini")
case ".tpl", ".tmpl", ".gotmpl":
return lexers.Get("go-text-template")
}
// 没有扩展名时看 shebang / 内容猜
if l := lexers.Analyse(src); l != nil {
return l
}
return lexers.Fallback
}
func highlight(name, src string) (string, string, error) {
lexer := chroma.Coalesce(pickLexer(name, src))
it, err := lexer.Tokenise(nil, src)
if err != nil {
return "", "", err
}
var buf bytes.Buffer
if err := codeFormatter.Format(&buf, styles.Fallback, it); err != nil {
return "", "", err
}
return buf.String(), lexer.Config().Name, nil
}
// ---------- Markdown(goldmark + GFM) ----------
var md = goldmark.New(
goldmark.WithExtensions(
extension.GFM, // 表格、删除线、任务列表、自动链接
extension.Footnote,
highlighting.NewHighlighting(
highlighting.WithFormatOptions(chromahtml.WithClasses(true)),
highlighting.WithGuessLanguage(false),
highlighting.WithWrapperRenderer(codeBlockWrapper),
),
),
goldmark.WithParserOptions(parser.WithAutoHeadingID()),
goldmark.WithRendererOptions(html.WithUnsafe()), // 本地笔记,允许内嵌 HTML
)
type outlineItem struct {
Level int `json:"level"`
Text string `json:"text"`
ID string `json:"id,omitempty"` // Markdown 标题锚点
Line int `json:"line,omitempty"` // 代码符号所在行
}
var frontMatterRe = regexp.MustCompile(`(?s)\A---\r?\n(.*?)\r?\n---\r?\n`)
func renderMarkdown(src []byte) (string, []outlineItem, error) {
var front string
lineOffset := 0
if m := frontMatterRe.FindSubmatch(src); m != nil {
front = string(m[1])
lineOffset = bytes.Count(m[0], []byte("\n"))
src = src[len(m[0]):]
}
ctx := parser.NewContext(parser.WithIDs(newGitHubIDs()))
doc := md.Parser().Parse(text.NewReader(src), parser.WithContext(ctx))
annotateSourceLines(doc, src, lineOffset)
var outline []outlineItem
ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if h, ok := n.(*ast.Heading); ok && entering {
id, _ := h.AttributeString("id")
idStr := ""
if b, ok := id.([]byte); ok {
idStr = string(b)
}
outline = append(outline, outlineItem{Level: h.Level, Text: nodeText(h, src), ID: idStr})
return ast.WalkSkipChildren, nil
}
return ast.WalkContinue, nil
})
var buf bytes.Buffer
if front != "" {
h, _, _ := highlight("front.yaml", front)
buf.WriteString(`<div class="front-matter">` + h + `</div>`)
}
if err := md.Renderer().Render(&buf, src, doc); err != nil {
return "", nil, err
}
return buf.String(), outline, nil
}
// annotateSourceLines 给块级元素加上 data-sl / data-el(对应源码的起止行号,从 1 开始),
// 前端据此把"相比首次打开的改动"标到渲染后的 Markdown 上。
func annotateSourceLines(doc ast.Node, src []byte, lineOffset int) {
starts := []int{0}
for i, c := range src {
if c == '\n' {
starts = append(starts, i+1)
}
}
lineOf := func(off int) int { return sort.SearchInts(starts, off+1) + lineOffset }
var span func(n ast.Node) (int, int)
span = func(n ast.Node) (int, int) {
lo, hi := -1, -1
add := func(a, b int) {
if a < 0 {
return
}
if lo < 0 || a < lo {
lo = a
}
if b > hi {
hi = b
}
}
if n.Type() == ast.TypeBlock {
ls := n.Lines()
for i := 0; i < ls.Len(); i++ {
seg := ls.At(i)
add(seg.Start, seg.Stop)
}
}
if t, ok := n.(*ast.Text); ok {
add(t.Segment.Start, t.Segment.Stop)
}
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
add(span(c))
}
if lo >= 0 && wantsLineAttr(n) {
sl, el := lineOf(lo), lineOf(max(lo, hi-1))
if n.Kind() == ast.KindFencedCodeBlock { // 内容行不含 ``` 两行
sl, el = sl-1, el+1
}
n.SetAttributeString("data-sl", []byte(strconv.Itoa(sl)))
n.SetAttributeString("data-el", []byte(strconv.Itoa(el)))
}
return lo, hi
}
span(doc)
}
func wantsLineAttr(n ast.Node) bool {
switch n.Kind() {
case ast.KindParagraph, ast.KindHeading, ast.KindFencedCodeBlock, ast.KindCodeBlock,
ast.KindHTMLBlock, ast.KindListItem, extast.KindTableRow, extast.KindTableHeader:
return true
}
return false
}
// codeBlockWrapper 用 div 包住代码块,带上行号属性。
func codeBlockWrapper(w util.BufWriter, c highlighting.CodeBlockContext, entering bool) {
if entering {
w.WriteString(`<div class="md-code"`)
if attrs := c.Attributes(); attrs != nil {
for _, name := range []string{"data-sl", "data-el"} {
if v, ok := attrs.GetString(name); ok {
if b, ok := v.([]byte); ok {
fmt.Fprintf(w, ` %s="%s"`, name, b)
}
}
}
}
w.WriteString(">")
if !c.Highlighted() {
w.WriteString("<pre><code>")
}
return
}
if !c.Highlighted() {
w.WriteString("</code></pre>")
}
w.WriteString("</div>\n")
}
func nodeText(n ast.Node, src []byte) string {
var b strings.Builder
ast.Walk(n, func(c ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
switch t := c.(type) {
case *ast.Text:
b.Write(t.Segment.Value(src))
case *ast.String:
b.Write(t.Value)
case *ast.CodeSpan:
for cc := t.FirstChild(); cc != nil; cc = cc.NextSibling() {
if tt, ok := cc.(*ast.Text); ok {
b.Write(tt.Segment.Value(src))
}
}
return ast.WalkSkipChildren, nil
}
return ast.WalkContinue, nil
})
return strings.TrimSpace(b.String())
}
// githubIDs 生成和 GitHub 一致的标题锚点:保留中文等 Unicode 字母,
// 空格变 -,去掉标点;重复的加 -1、-2 后缀。goldmark 默认会丢掉中文。
type githubIDs struct{ seen map[string]int }
func newGitHubIDs() *githubIDs { return &githubIDs{seen: map[string]int{}} }
func (g *githubIDs) Generate(value []byte, kind ast.NodeKind) []byte {
var b strings.Builder
for _, r := range strings.ToLower(strings.TrimSpace(string(value))) {
switch {
case unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '_':
b.WriteRune(r)
case r == ' ':
b.WriteByte('-')
}
}
id := b.String()
if id == "" {
id = "heading"
}
base := id
for n := 1; g.seen[id] > 0; n++ {
id = fmt.Sprintf("%s-%d", base, n)
}
g.seen[id]++
return []byte(id)
}
func (g *githubIDs) Put(value []byte) { g.seen[string(value)]++ }
// ---------- 代码大纲(右侧"文件结构") ----------
// 用正则粗略提取,够浏览笔记 / 脚本用;不追求完整的语法分析。
type outlineRule struct {
re *regexp.Regexp
level func(m []string) int
}
func indentLevel(s string) int { return len(strings.ReplaceAll(s, "\t", " "))/2 + 1 }
var outlineRules = map[string][]outlineRule{
"Go": {
{re: regexp.MustCompile(`^func\s+(\([^)]*\)\s*)?([A-Za-z_]\w*)`)},
{re: regexp.MustCompile(`^type\s+([A-Za-z_]\w*)\s+(struct|interface|func|\w)`)},
},
"Python": {
{re: regexp.MustCompile(`^(\s*)(?:async\s+)?(?:def|class)\s+([A-Za-z_]\w*)`), level: func(m []string) int { return len(m[1])/4 + 1 }},
},
"Bash": {
{re: regexp.MustCompile(`^\s*(?:function\s+)?([A-Za-z_][\w:-]*)\s*\(\)\s*\{?`)},
{re: regexp.MustCompile(`^\s*function\s+([A-Za-z_][\w:-]*)`)},
},
"YAML": {
// 只取前两层 key,k8s 清单里 apiVersion/kind/metadata/spec 一目了然
{re: regexp.MustCompile(`^(\s{0,2})([A-Za-z_][\w.-]*):(\s|$)`), level: func(m []string) int { return len(m[1])/2 + 1 }},
{re: regexp.MustCompile(`^(---)`)},
},
"JavaScript": {
{re: regexp.MustCompile(`^\s*(?:export\s+)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)`)},
{re: regexp.MustCompile(`^\s*(?:export\s+)?class\s+([A-Za-z_$][\w$]*)`)},
},
}
func init() {
outlineRules["TypeScript"] = outlineRules["JavaScript"]
outlineRules["Shell"] = outlineRules["Bash"]
}
func codeOutline(name, lang, src string) []outlineItem {
rules, ok := outlineRules[lang]
if !ok {
return nil
}
var out []outlineItem
for i, line := range strings.Split(src, "\n") {
for _, r := range rules {
m := r.re.FindStringSubmatch(line)
if m == nil {
continue
}
level := 1
if r.level != nil {
level = r.level(m)
}
label := m[len(m)-1]
switch lang {
case "Go":
label = strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(line), "{"))
if len(label) > 80 {
label = label[:80] + "…"
}
case "YAML":
if m[1] == "---" {
label = "---"
} else {
label = m[2]
}
case "Python", "Bash", "Shell":
label = strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(line), ":"))
}
out = append(out, outlineItem{Level: level, Text: label, Line: i + 1})
break
}
if len(out) >= 500 {
break
}
}
return out
}
// naturalLess 自然排序:file2 排在 file10 前面,01-intro 和 02-arch 按数字排。
func naturalLess(a, b string) bool {
for a != "" && b != "" {
ca, cb := a[0], b[0]
if isDigit(ca) && isDigit(cb) {
na, ra := splitNum(a)
nb, rb := splitNum(b)
ta, tb := strings.TrimLeft(na, "0"), strings.TrimLeft(nb, "0")
if len(ta) != len(tb) {
return len(ta) < len(tb)
}
if ta != tb {
return ta < tb
}
a, b = ra, rb
continue
}
if ca != cb {
return ca < cb
}
a, b = a[1:], b[1:]
}
return len(a) < len(b)
}
func isDigit(c byte) bool { return c >= '0' && c <= '9' }
func splitNum(s string) (string, string) {
i := 0
for i < len(s) && isDigit(s[i]) {
i++
}
return s[:i], s[i:]
}