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
+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)
})
}