chore: remove unused syntax

- Removed the wikilink extension from markdown services in test and API service.
- Deleted the DefaultLink and WikiLink components, simplifying link handling.
- Updated ConditionalComponent to remove wikilink checks.
- Adjusted MemoContent to exclude wikilink handling in markdown rendering.
- Refined markdown styles for compact rendering, enhancing readability.
- Added a Markdown Styling Guide to document the new compact styling approach.
This commit is contained in:
Claude
2025-10-27 08:31:57 +08:00
parent 7eec424274
commit 596b894ca0
22 changed files with 370 additions and 764 deletions
-32
View File
@@ -1,32 +0,0 @@
package ast
import (
gast "github.com/yuin/goldmark/ast"
)
// WikilinkNode represents [[target]] or [[target?params]] syntax.
type WikilinkNode struct {
gast.BaseInline
// Target is the link destination (e.g., "memos/1", "Hello world", "resources/101")
Target []byte
// Params are optional parameters (e.g., "align=center" from [[target?align=center]])
Params []byte
}
// KindWikilink is the NodeKind for WikilinkNode.
var KindWikilink = gast.NewNodeKind("Wikilink")
// Kind returns KindWikilink.
func (*WikilinkNode) Kind() gast.NodeKind {
return KindWikilink
}
// Dump implements Node.Dump for debugging.
func (n *WikilinkNode) Dump(source []byte, level int) {
gast.DumpHelper(n, source, level, map[string]string{
"Target": string(n.Target),
"Params": string(n.Params),
}, nil)
}
-24
View File
@@ -1,24 +0,0 @@
package extensions
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/util"
mparser "github.com/usememos/memos/plugin/markdown/parser"
)
type wikilinkExtension struct{}
// WikilinkExtension is a goldmark extension for [[...]] wikilink syntax.
var WikilinkExtension = &wikilinkExtension{}
// Extend extends the goldmark parser with wikilink support.
func (*wikilinkExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithInlineParsers(
// Priority 199 - run before standard link parser (500) but after tags (200)
util.Prioritized(mparser.NewWikilinkParser(), 199),
),
)
}
+3 -14
View File
@@ -62,8 +62,7 @@ type service struct {
type Option func(*config)
type config struct {
enableTags bool
enableWikilink bool
enableTags bool
}
// WithTagExtension enables #tag parsing.
@@ -73,13 +72,6 @@ func WithTagExtension() Option {
}
}
// WithWikilinkExtension enables [[wikilink]] parsing.
func WithWikilinkExtension() Option {
return func(c *config) {
c.enableWikilink = true
}
}
// NewService creates a new markdown service with the given options.
func NewService(opts ...Option) Service {
cfg := &config{}
@@ -95,9 +87,6 @@ func NewService(opts ...Option) Service {
if cfg.enableTags {
exts = append(exts, extensions.TagExtension)
}
if cfg.enableWikilink {
exts = append(exts, extensions.WikilinkExtension)
}
md := goldmark.New(
goldmark.WithExtensions(exts...),
@@ -164,7 +153,7 @@ func (s *service) ExtractProperties(content []byte) (*storepb.MemoPayload_Proper
}
switch n.Kind() {
case gast.KindLink, mast.KindWikilink:
case gast.KindLink:
prop.HasLink = true
case gast.KindCodeBlock, gast.KindFencedCodeBlock, gast.KindCodeSpan:
@@ -321,7 +310,7 @@ func (s *service) ExtractAll(content []byte) (*ExtractedData, error) {
// Extract properties based on node kind
switch n.Kind() {
case gast.KindLink, mast.KindWikilink:
case gast.KindLink:
data.Property.HasLink = true
case gast.KindCodeBlock, gast.KindFencedCodeBlock, gast.KindCodeSpan:
+1 -24
View File
@@ -109,7 +109,6 @@ func TestExtractProperties(t *testing.T) {
tests := []struct {
name string
content string
withExt bool
hasLink bool
hasCode bool
hasTasks bool
@@ -118,7 +117,6 @@ func TestExtractProperties(t *testing.T) {
{
name: "plain text",
content: "Just plain text",
withExt: false,
hasLink: false,
hasCode: false,
hasTasks: false,
@@ -127,7 +125,6 @@ func TestExtractProperties(t *testing.T) {
{
name: "with link",
content: "Check out [this link](https://example.com)",
withExt: false,
hasLink: true,
hasCode: false,
hasTasks: false,
@@ -136,7 +133,6 @@ func TestExtractProperties(t *testing.T) {
{
name: "with inline code",
content: "Use `console.log()` to debug",
withExt: false,
hasLink: false,
hasCode: true,
hasTasks: false,
@@ -145,7 +141,6 @@ func TestExtractProperties(t *testing.T) {
{
name: "with code block",
content: "```go\nfunc main() {}\n```",
withExt: false,
hasLink: false,
hasCode: true,
hasTasks: false,
@@ -154,7 +149,6 @@ func TestExtractProperties(t *testing.T) {
{
name: "with completed task",
content: "- [x] Completed task",
withExt: false,
hasLink: false,
hasCode: false,
hasTasks: true,
@@ -163,7 +157,6 @@ func TestExtractProperties(t *testing.T) {
{
name: "with incomplete task",
content: "- [ ] Todo item",
withExt: false,
hasLink: false,
hasCode: false,
hasTasks: true,
@@ -172,25 +165,14 @@ func TestExtractProperties(t *testing.T) {
{
name: "mixed tasks",
content: "- [x] Done\n- [ ] Not done",
withExt: false,
hasLink: false,
hasCode: false,
hasTasks: true,
hasInc: true,
},
{
name: "with referenced content",
content: "See [[memos/1]] for details",
withExt: true,
hasLink: true,
hasCode: false,
hasTasks: false,
hasInc: false,
},
{
name: "everything",
content: "# Title\n\n[Link](url)\n\n`code`\n\n- [ ] Task",
withExt: false,
hasLink: true,
hasCode: true,
hasTasks: true,
@@ -200,12 +182,7 @@ func TestExtractProperties(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var svc Service
if tt.withExt {
svc = NewService(WithWikilinkExtension())
} else {
svc = NewService()
}
svc := NewService()
props, err := svc.ExtractProperties([]byte(tt.content))
require.NoError(t, err)
+2 -2
View File
@@ -45,7 +45,7 @@ func (*tagParser) Parse(_ gast.Node, block text.Reader, _ parser.Context) gast.N
}
// Scan tag characters
// Valid: alphanumeric, dash, underscore
// Valid: alphanumeric, dash, underscore, forward slash
tagEnd := 1 // Start after #
for tagEnd < len(line) {
c := line[tagEnd]
@@ -53,7 +53,7 @@ func (*tagParser) Parse(_ gast.Node, block text.Reader, _ parser.Context) gast.N
isValid := (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_'
c == '-' || c == '_' || c == '/'
if !isValid {
break
+30
View File
@@ -96,6 +96,36 @@ func TestTagParser(t *testing.T) {
expectedTag: "WorkNotes",
shouldParse: true,
},
{
name: "hierarchical tag with slash",
input: "#tag1/subtag",
expectedTag: "tag1/subtag",
shouldParse: true,
},
{
name: "hierarchical tag with multiple levels",
input: "#tag1/subtag/subtag2",
expectedTag: "tag1/subtag/subtag2",
shouldParse: true,
},
{
name: "hierarchical tag followed by space",
input: "#work/notes ",
expectedTag: "work/notes",
shouldParse: true,
},
{
name: "hierarchical tag followed by punctuation",
input: "#project/2024.",
expectedTag: "project/2024",
shouldParse: true,
},
{
name: "hierarchical tag with numbers and dashes",
input: "#work-log/2024/q1",
expectedTag: "work-log/2024/q1",
shouldParse: true,
},
}
for _, tt := range tests {
-104
View File
@@ -1,104 +0,0 @@
package parser
import (
"bytes"
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
mast "github.com/usememos/memos/plugin/markdown/ast"
)
type wikilinkParser struct{}
// NewWikilinkParser creates a new inline parser for [[...]] wikilink syntax.
func NewWikilinkParser() parser.InlineParser {
return &wikilinkParser{}
}
// Trigger returns the characters that trigger this parser.
func (*wikilinkParser) Trigger() []byte {
return []byte{'['}
}
// Parse parses [[target]] or [[target?params]] wikilink syntax.
func (*wikilinkParser) Parse(_ gast.Node, block text.Reader, _ parser.Context) gast.Node {
line, _ := block.PeekLine()
// Must start with [[
if len(line) < 2 || line[0] != '[' || line[1] != '[' {
return nil
}
// Find closing ]]
closePos := findClosingBrackets(line[2:])
if closePos == -1 {
return nil
}
// Extract content between [[ and ]]
// closePos is relative to line[2:], so actual position is closePos + 2
contentStart := 2
contentEnd := contentStart + closePos
content := line[contentStart:contentEnd]
// Empty content is not allowed
if len(bytes.TrimSpace(content)) == 0 {
return nil
}
// Parse target and parameters
target, params := parseTargetAndParams(content)
// Advance reader position
// +2 for [[, +len(content), +2 for ]]
block.Advance(contentEnd + 2)
// Create AST node
node := &mast.WikilinkNode{
Target: target,
Params: params,
}
return node
}
// findClosingBrackets finds the position of ]] in the byte slice.
// Returns -1 if not found.
func findClosingBrackets(data []byte) int {
for i := 0; i < len(data)-1; i++ {
if data[i] == ']' && data[i+1] == ']' {
return i
}
}
return -1
}
// parseTargetAndParams splits content on ? to extract target and parameters.
func parseTargetAndParams(content []byte) (target []byte, params []byte) {
// Find ? separator
idx := bytes.IndexByte(content, '?')
if idx == -1 {
// No parameters
target = bytes.TrimSpace(content)
return target, nil
}
// Split on ?
target = bytes.TrimSpace(content[:idx])
params = content[idx+1:] // Keep params as-is (don't trim, might have meaningful spaces)
// Make copies to avoid issues with slice sharing
targetCopy := make([]byte, len(target))
copy(targetCopy, target)
var paramsCopy []byte
if len(params) > 0 {
paramsCopy = make([]byte, len(params))
copy(paramsCopy, params)
}
return targetCopy, paramsCopy
}
-252
View File
@@ -1,252 +0,0 @@
package parser
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
mast "github.com/usememos/memos/plugin/markdown/ast"
)
func TestWikilinkParser(t *testing.T) {
tests := []struct {
name string
input string
expectedTarget string
expectedParams string
shouldParse bool
}{
{
name: "basic wikilink",
input: "[[Hello world]]",
expectedTarget: "Hello world",
expectedParams: "",
shouldParse: true,
},
{
name: "memo wikilink",
input: "[[memos/1]]",
expectedTarget: "memos/1",
expectedParams: "",
shouldParse: true,
},
{
name: "resource wikilink",
input: "[[resources/101]]",
expectedTarget: "resources/101",
expectedParams: "",
shouldParse: true,
},
{
name: "with parameters",
input: "[[resources/101?align=center]]",
expectedTarget: "resources/101",
expectedParams: "align=center",
shouldParse: true,
},
{
name: "multiple parameters",
input: "[[resources/101?align=center&width=300]]",
expectedTarget: "resources/101",
expectedParams: "align=center&width=300",
shouldParse: true,
},
{
name: "inline with text after",
input: "[[resources/101]]111",
expectedTarget: "resources/101",
expectedParams: "",
shouldParse: true,
},
{
name: "whitespace trimmed",
input: "[[ Hello world ]]",
expectedTarget: "Hello world",
expectedParams: "",
shouldParse: true,
},
{
name: "empty content",
input: "[[]]",
expectedTarget: "",
expectedParams: "",
shouldParse: false,
},
{
name: "whitespace only",
input: "[[ ]]",
expectedTarget: "",
expectedParams: "",
shouldParse: false,
},
{
name: "missing closing brackets",
input: "[[Hello world",
expectedTarget: "",
expectedParams: "",
shouldParse: false,
},
{
name: "single bracket",
input: "[Hello]",
expectedTarget: "",
expectedParams: "",
shouldParse: false,
},
{
name: "nested brackets",
input: "[[outer [[inner]] ]]",
expectedTarget: "outer [[inner",
expectedParams: "",
shouldParse: true, // Stops at first ]]
},
{
name: "special characters",
input: "[[Project/2024/Notes]]",
expectedTarget: "Project/2024/Notes",
expectedParams: "",
shouldParse: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewWikilinkParser()
reader := text.NewReader([]byte(tt.input))
ctx := parser.NewContext()
node := p.Parse(nil, reader, ctx)
if tt.shouldParse {
require.NotNil(t, node, "Expected wikilink to be parsed")
require.IsType(t, &mast.WikilinkNode{}, node)
wikilinkNode, ok := node.(*mast.WikilinkNode)
require.True(t, ok, "Expected node to be *mast.WikilinkNode")
assert.Equal(t, tt.expectedTarget, string(wikilinkNode.Target))
assert.Equal(t, tt.expectedParams, string(wikilinkNode.Params))
} else {
assert.Nil(t, node, "Expected wikilink NOT to be parsed")
}
})
}
}
func TestWikilinkParser_Trigger(t *testing.T) {
p := NewWikilinkParser()
triggers := p.Trigger()
assert.Equal(t, []byte{'['}, triggers)
}
func TestFindClosingBrackets(t *testing.T) {
tests := []struct {
name string
input []byte
expected int
}{
{
name: "simple case",
input: []byte("hello]]world"),
expected: 5,
},
{
name: "not found",
input: []byte("hello world"),
expected: -1,
},
{
name: "at start",
input: []byte("]]hello"),
expected: 0,
},
{
name: "single bracket",
input: []byte("hello]world"),
expected: -1,
},
{
name: "empty",
input: []byte(""),
expected: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := findClosingBrackets(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}
func TestParseTargetAndParams(t *testing.T) {
tests := []struct {
name string
input []byte
expectedTarget string
expectedParams string
}{
{
name: "no params",
input: []byte("target"),
expectedTarget: "target",
expectedParams: "",
},
{
name: "with params",
input: []byte("target?param=value"),
expectedTarget: "target",
expectedParams: "param=value",
},
{
name: "multiple params",
input: []byte("target?a=1&b=2"),
expectedTarget: "target",
expectedParams: "a=1&b=2",
},
{
name: "whitespace trimmed from target",
input: []byte(" target ?param=value"),
expectedTarget: "target",
expectedParams: "param=value",
},
{
name: "empty params",
input: []byte("target?"),
expectedTarget: "target",
expectedParams: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
target, params := parseTargetAndParams(tt.input)
assert.Equal(t, tt.expectedTarget, string(target))
assert.Equal(t, tt.expectedParams, string(params))
})
}
}
func TestWikilinkNode_Kind(t *testing.T) {
node := &mast.WikilinkNode{
Target: []byte("test"),
}
assert.Equal(t, mast.KindWikilink, node.Kind())
}
func TestWikilinkNode_Dump(t *testing.T) {
node := &mast.WikilinkNode{
Target: []byte("test"),
Params: []byte("param=value"),
}
// Should not panic
assert.NotPanics(t, func() {
node.Dump([]byte("[[test?param=value]]"), 0)
})
}
@@ -156,15 +156,6 @@ func (r *MarkdownRenderer) renderNode(node gast.Node, source []byte, depth int)
r.buf.WriteByte('#')
r.buf.Write(n.Tag)
case *mast.WikilinkNode:
r.buf.WriteString("[[")
r.buf.Write(n.Target)
if len(n.Params) > 0 {
r.buf.WriteByte('?')
r.buf.Write(n.Params)
}
r.buf.WriteString("]]")
default:
// For unknown nodes, try to render children
r.renderChildren(n, source, depth)
@@ -19,7 +19,6 @@ func TestMarkdownRenderer(t *testing.T) {
goldmark.WithExtensions(
extension.GFM,
extensions.TagExtension,
extensions.WikilinkExtension,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
@@ -111,15 +110,10 @@ func TestMarkdownRenderer(t *testing.T) {
input: "#work #important meeting notes",
expected: "#work #important meeting notes",
},
{
name: "referenced content (wikilink)",
input: "Check [[memos/42]] for details",
expected: "Check [[memos/42]] for details",
},
{
name: "complex mixed content",
input: "# Meeting Notes\n\n**Date**: 2024-01-01\n\n## Attendees\n- Alice\n- Bob\n\n## Discussion\n\nWe discussed #project status.\n\nSee [[memos/1]] for background.\n\n```python\nprint('hello')\n```",
expected: "# Meeting Notes\n\n**Date**: 2024-01-01\n\n## Attendees\n\n- Alice\n- Bob\n\n## Discussion\n\nWe discussed #project status.\n\nSee [[memos/1]] for background.\n\n```python\nprint('hello')\n```",
input: "# Meeting Notes\n\n**Date**: 2024-01-01\n\n## Attendees\n- Alice\n- Bob\n\n## Discussion\n\nWe discussed #project status.\n\n```python\nprint('hello')\n```",
expected: "# Meeting Notes\n\n**Date**: 2024-01-01\n\n## Attendees\n\n- Alice\n- Bob\n\n## Discussion\n\nWe discussed #project status.\n\n```python\nprint('hello')\n```",
},
}
@@ -153,7 +147,6 @@ func TestMarkdownRendererPreservesStructure(t *testing.T) {
goldmark.WithExtensions(
extension.GFM,
extensions.TagExtension,
extensions.WikilinkExtension,
),
)
@@ -162,7 +155,6 @@ func TestMarkdownRendererPreservesStructure(t *testing.T) {
"**Bold** and *italic*",
"- List\n- Items",
"#tag #another",
"[[wikilink]]",
"> Quote",
}