mirror of
https://github.com/kovetskiy/mark.git
synced 2026-05-03 14:47:38 +08:00
feat: integrate goldmark Pos() for better error reporting
This commit is contained in:
parent
cad33f5097
commit
5c59e4704b
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/kovetskiy/mark/v16/mermaid"
|
"github.com/kovetskiy/mark/v16/mermaid"
|
||||||
"github.com/kovetskiy/mark/v16/stdlib"
|
"github.com/kovetskiy/mark/v16/stdlib"
|
||||||
"github.com/kovetskiy/mark/v16/types"
|
"github.com/kovetskiy/mark/v16/types"
|
||||||
"github.com/reconquest/pkg/log"
|
|
||||||
|
|
||||||
"github.com/yuin/goldmark/ast"
|
"github.com/yuin/goldmark/ast"
|
||||||
"github.com/yuin/goldmark/renderer"
|
"github.com/yuin/goldmark/renderer"
|
||||||
@ -135,8 +134,8 @@ func (r *ConfluenceFencedCodeBlockRenderer) renderFencedCodeBlock(writer util.Bu
|
|||||||
if lang == "d2" && slices.Contains(r.MarkConfig.Features, "d2") {
|
if lang == "d2" && slices.Contains(r.MarkConfig.Features, "d2") {
|
||||||
attachment, err := d2.ProcessD2(title, lval, r.MarkConfig.D2Scale)
|
attachment, err := d2.ProcessD2(title, lval, r.MarkConfig.D2Scale)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf(nil, "error: %v", err)
|
line, col := GetLineCol(source, node.Pos())
|
||||||
return ast.WalkStop, err
|
return ast.WalkStop, fmt.Errorf("line %d, col %d: d2 rendering failed: %v", line, col, err)
|
||||||
}
|
}
|
||||||
r.Attachments.Attach(attachment)
|
r.Attachments.Attach(attachment)
|
||||||
|
|
||||||
@ -179,8 +178,8 @@ func (r *ConfluenceFencedCodeBlockRenderer) renderFencedCodeBlock(writer util.Bu
|
|||||||
} else if lang == "mermaid" && slices.Contains(r.MarkConfig.Features, "mermaid") {
|
} else if lang == "mermaid" && slices.Contains(r.MarkConfig.Features, "mermaid") {
|
||||||
attachment, err := mermaid.ProcessMermaidLocally(title, lval, r.MarkConfig.MermaidScale)
|
attachment, err := mermaid.ProcessMermaidLocally(title, lval, r.MarkConfig.MermaidScale)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf(nil, "error: %v", err)
|
line, col := GetLineCol(source, node.Pos())
|
||||||
return ast.WalkStop, err
|
return ast.WalkStop, fmt.Errorf("line %d, col %d: mermaid rendering failed: %v", line, col, err)
|
||||||
}
|
}
|
||||||
r.Attachments.Attach(attachment)
|
r.Attachments.Attach(attachment)
|
||||||
|
|
||||||
|
|||||||
@ -145,7 +145,8 @@ func (r *ConfluenceImageRenderer) renderImage(writer util.BufWriter, source []by
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
if len(attachments) == 0 {
|
if len(attachments) == 0 {
|
||||||
return ast.WalkStop, fmt.Errorf("no attachment resolved for %q", string(n.Destination))
|
line, col := GetLineCol(source, node.Pos())
|
||||||
|
return ast.WalkStop, fmt.Errorf("line %d, col %d: no attachment resolved for %q", line, col, string(n.Destination))
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Attachments.Attach(attachments[0])
|
r.Attachments.Attach(attachments[0])
|
||||||
|
|||||||
19
renderer/util.go
Normal file
19
renderer/util.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package renderer
|
||||||
|
|
||||||
|
// GetLineCol returns the 1-based line and column for a given byte offset in the source.
|
||||||
|
func GetLineCol(source []byte, offset int) (line, col int) {
|
||||||
|
line = 1
|
||||||
|
col = 1
|
||||||
|
if offset > len(source) {
|
||||||
|
offset = len(source)
|
||||||
|
}
|
||||||
|
for i := 0; i < offset; i++ {
|
||||||
|
if source[i] == '\n' {
|
||||||
|
line++
|
||||||
|
col = 1
|
||||||
|
} else {
|
||||||
|
col++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return line, col
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user