fix: restore nested Markdown lists and use object routes for Wiki links
This commit is contained in:
1 parent
38b0fe2a94
commit
2cbaa9342d
7 files changed
+73
-7
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -13,8 +14,24 @@ import (
|
||||
|
||||
// HTMLToMarkdownAuthoring keeps source HTML when conversion loses structure,
|
||||
// attributes, comments or text. This is not a sanitization boundary.
|
||||
var nestedListGap = regexp.MustCompile(`\n[\t ]*\n([\t ]+(?:[-+*]|[0-9]+\.) )`)
|
||||
|
||||
func authoringMarkdown(source string) (string, error) {
|
||||
md, err := HTMLToMarkdown(source)
|
||||
if err != nil {
|
||||
return md, err
|
||||
}
|
||||
// The converter inserts a loose-list gap before nested lists. Only accept
|
||||
// tightening when the resulting DOM matches; code and real paragraphs stay protected.
|
||||
tight := nestedListGap.ReplaceAllString(md, "\n$1")
|
||||
if tight != md && htmlStructure(source) == htmlStructure(MarkdownToHTML(tight)) {
|
||||
return tight, nil
|
||||
}
|
||||
return md, nil
|
||||
}
|
||||
|
||||
func HTMLToMarkdownAuthoring(source string) (string, error) {
|
||||
converted, err := HTMLToMarkdown(source)
|
||||
converted, err := authoringMarkdown(source)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -30,7 +47,7 @@ func HTMLToMarkdownAuthoring(source string) (string, error) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
continue
|
||||
}
|
||||
md, e := HTMLToMarkdown(raw)
|
||||
md, e := authoringMarkdown(raw)
|
||||
if e != nil || htmlStructure(raw) != htmlStructure(MarkdownToHTML(md)) {
|
||||
md = rawHTMLBlock(raw)
|
||||
}
|
||||
@@ -171,6 +188,22 @@ func htmlStructure(source string) string {
|
||||
literal = literal || n.Data == "pre" || n.Data == "code" || n.Data == "script" || n.Data == "style"
|
||||
data := n.Data
|
||||
if n.Type == html.TextNode && !literal {
|
||||
// Formatting whitespace beside a nested block is not an inline word
|
||||
// separator. Keep all whitespace between inline siblings significant.
|
||||
if n.Parent != nil && n.Parent.Data == "li" {
|
||||
isList := func(x *html.Node) bool {
|
||||
return x != nil && x.Type == html.ElementNode && (x.Data == "ul" || x.Data == "ol")
|
||||
}
|
||||
if isList(n.NextSibling) {
|
||||
data = strings.TrimRight(data, " \t\r\n")
|
||||
}
|
||||
if isList(n.PrevSibling) {
|
||||
data = strings.TrimLeft(data, " \t\r\n")
|
||||
}
|
||||
if data == "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(data) == "" && n.Parent != nil {
|
||||
switch n.Parent.Data {
|
||||
case "ul", "ol", "table", "thead", "tbody", "tfoot", "tr":
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package grammaratur
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAuthoringNestedLists(t *testing.T) {
|
||||
for _, source := range []string{
|
||||
"<ul><li>Parent<ul><li>Child</li></ul></li></ul>",
|
||||
"<ul>\n<li>Parent\n<ul>\n<li>Child</li>\n</ul>\n</li>\n</ul>",
|
||||
"<ul><li>Parent<ul><li>Child<ul><li>Grandchild</li></ul></li></ul></li><li>Sibling</li></ul>",
|
||||
"<ol><li>Parent<ul><li>Child</li></ul></li></ol>",
|
||||
"<ul><li><strong>Parent</strong><ul><li>Child</li></ul></li></ul>",
|
||||
} {
|
||||
md, err := HTMLToMarkdownAuthoring(source)
|
||||
if htmlStructure(source) != htmlStructure(MarkdownAuthoringHTML(md)) {
|
||||
t.Errorf("list structure changed: %q", md)
|
||||
}
|
||||
if err != nil || strings.Contains(md, "{=html}") {
|
||||
plain, _ := HTMLToMarkdown(source)
|
||||
t.Errorf("source=%q md=%q plain=%q rendered=%q err=%v", source, md, plain, MarkdownToHTML(plain), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthoringNestedListPreservesAttributes(t *testing.T) {
|
||||
source := `<ul><li data-custom="keep">Parent<ul><li>Child</li></ul></li></ul>`
|
||||
md, err := HTMLToMarkdownAuthoring(source)
|
||||
if err != nil || MarkdownAuthoringHTML(md) != source {
|
||||
t.Fatalf("protected list changed: %q %v", md, err)
|
||||
}
|
||||
}
|
||||
Vendored
+1
-1
@@ -27,7 +27,7 @@
|
||||
d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body'));
|
||||
d.querySelector('#feed-reader .feed-actions button:last-child').click();await wait(()=>d.querySelector('#feed-reader form'));
|
||||
assert(!['rgba(0, 0, 0, 0)','transparent'].includes(w.getComputedStyle(d.querySelector('.feed-capture')).backgroundColor),'Capture dialog background transparent');
|
||||
const form=d.querySelector('#feed-reader form'),inputs=form.querySelectorAll('input');assert(inputs[2].value==='/notes/{YYYY}/{MM}/{DD}/{Subject}','Wiki default not filled');assert(form.closest('dialog').open&&d.querySelector('#feed-reader .feed-body'),'Capture replaced article');assert(form.querySelector('a[href="/-/sys/config?panel=organizer"]'),'Default settings link missing');await wait(()=>form.querySelector('output').textContent.includes('/rss-'));assert(!form.querySelector('output').textContent.includes('{'),'Template not expanded');inputs[2].value='/notes/feed-ui';inputs[2].dispatchEvent(new w.Event('input'));await wait(()=>form.querySelector('output').textContent.startsWith('/notes/feed-ui/rss-'));inputs[5].checked=true;form.requestSubmit();await wait(()=>d.querySelector('#feed-reader a[href^="/v/uuid:"]'));d.querySelector('.feed-capture button').click();await wait(()=>!d.querySelector('.feed-capture'));assert(d.querySelector('#feed-reader .feed-body'),'Closing capture lost article');
|
||||
const form=d.querySelector('#feed-reader form'),inputs=form.querySelectorAll('input');assert(inputs[2].value==='/notes/{YYYY}/{MM}/{DD}/{Subject}','Wiki default not filled');assert(form.closest('dialog').open&&d.querySelector('#feed-reader .feed-body'),'Capture replaced article');assert(form.querySelector('a[href="/-/sys/config?panel=organizer"]'),'Default settings link missing');await wait(()=>form.querySelector('output').textContent.includes('/rss-'));assert(!form.querySelector('output').textContent.includes('{'),'Template not expanded');inputs[2].value='/notes/feed-ui';inputs[2].dispatchEvent(new w.Event('input'));await wait(()=>form.querySelector('output').textContent.startsWith('/notes/feed-ui/rss-'));inputs[5].checked=true;form.requestSubmit();await wait(()=>d.querySelector('#feed-reader a[href^="/o/uuid:"]'));d.querySelector('.feed-capture button').click();await wait(()=>!d.querySelector('.feed-capture'));assert(d.querySelector('#feed-reader .feed-body'),'Closing capture lost article');
|
||||
const search=d.querySelector('#feed-toolbar input');search.value='';search.dispatchEvent(new w.Event('input',{bubbles:true}));await wait(()=>d.querySelectorAll('.feed-row').length===100);d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body'));
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'Reader overflow');
|
||||
d.getElementById('feed-opml').click();await wait(()=>d.getElementById('feed-opml-file'));
|
||||
|
||||
Reference in new issue
Block a user