Fix section linking and editing
This commit is contained in:
@@ -298,8 +298,7 @@ func (h *handler) renderCanvasFile(v *canvasNodeView, n canvasNode) {
|
||||
if heading := extractFirstHeading(raw); heading != "" {
|
||||
v.Label = heading
|
||||
}
|
||||
v.Markdown = true
|
||||
v.Body = template.HTML(convertMarkdown(canvasSubpath(raw, n.Subpath), dirBase(rel)))
|
||||
canvasMarkdownBody(v, raw, n.Subpath, dirBase(rel))
|
||||
return
|
||||
}
|
||||
u := fileURL(rel)
|
||||
@@ -309,8 +308,7 @@ func (h *handler) renderCanvasFile(v *canvasNodeView, n canvasNode) {
|
||||
switch {
|
||||
case ext == ".md":
|
||||
raw, _ := os.ReadFile(fsPath)
|
||||
v.Markdown = true
|
||||
v.Body = template.HTML(convertMarkdown(canvasSubpath(raw, n.Subpath), dirBase(path.Dir("/"+rel))))
|
||||
canvasMarkdownBody(v, raw, n.Subpath, dirBase(path.Dir("/"+rel)))
|
||||
case isImageFile(name) || ext == ".svg" || ext == ".webp":
|
||||
thumb := u
|
||||
if hasThumbnail(name) {
|
||||
@@ -336,21 +334,36 @@ func dirBase(rel string) string {
|
||||
return "/" + rel + "/"
|
||||
}
|
||||
|
||||
// canvasMarkdownBody renders a markdown file node: the whole file, or the
|
||||
// section its subpath names. A subpath that names no heading says so on the
|
||||
// card rather than showing the whole file (as jsoncanvas-desktop does).
|
||||
func canvasMarkdownBody(v *canvasNodeView, raw []byte, subpath, base string) {
|
||||
part, ok := canvasSubpath(raw, subpath)
|
||||
if !ok {
|
||||
v.Body = execNodeTemplate("canvas-missing", subpath)
|
||||
return
|
||||
}
|
||||
v.Markdown = true
|
||||
v.Body = template.HTML(convertMarkdown(part, base))
|
||||
}
|
||||
|
||||
// canvasSubpath narrows markdown to a file node's "#Heading" subpath: that
|
||||
// heading's section including its subsections. Unknown subpaths (or block
|
||||
// references, "#^id") show the whole file.
|
||||
func canvasSubpath(raw []byte, subpath string) []byte {
|
||||
// heading's section including its subsections, matched case-insensitively
|
||||
// on the heading text. Without a subpath it is the whole file; ok is false
|
||||
// for a subpath that names no heading, which includes block references
|
||||
// ("#^id").
|
||||
func canvasSubpath(raw []byte, subpath string) (part []byte, ok bool) {
|
||||
want := strings.TrimSpace(strings.TrimPrefix(subpath, "#"))
|
||||
if want == "" || strings.HasPrefix(want, "^") {
|
||||
return raw
|
||||
if want == "" {
|
||||
return raw, true
|
||||
}
|
||||
sections := splitSections(raw)
|
||||
for i, s := range sections {
|
||||
if _, text := sectionHeading(s); i > 0 && strings.EqualFold(text, want) {
|
||||
return joinSections(sections[i:secionSpanEnd(sections, i)])
|
||||
return joinSections(sections[i:secionSpanEnd(sections, i)]), true
|
||||
}
|
||||
}
|
||||
return raw
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// canvasTaskCheckboxes makes a text node's task checkboxes clickable. The
|
||||
|
||||
Reference in New Issue
Block a user