Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ func TestExtractTags(t *testing.T) {
withExt: true,
expected: []string{"tag"},
},
{
name: "tags require a boundary before the hash",
content: "word#tag 1#numeric word##tag ##tag (#parentheses) [#brackets] \"#quotes\" +#symbol",
withExt: true,
expected: []string{"parentheses", "brackets", "quotes", "symbol"},
},
{
name: "multiple tags",
content: "Text with #tag1 and #tag2",
Expand Down
9 changes: 9 additions & 0 deletions internal/markdown/parser/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func isValidTagRune(r rune) bool {
return false
}

func isTagBoundary(r rune) bool {
return r != '#' && (unicode.IsSpace(r) || unicode.IsPunct(r) || unicode.IsSymbol(r))
}

// Parse parses #tag syntax using Unicode-aware validation.
// Tags support international characters and follow these rules:
// - Must start with # followed by valid tag characters
Expand All @@ -84,6 +88,11 @@ func (*tagParser) Parse(_ gast.Node, block text.Reader, _ parser.Context) gast.N
return nil
}

prev := block.PrecendingCharacter()
if prev != '\n' && !isTagBoundary(prev) {
return nil
}

// Check if it's a heading (## or space after #)
if len(line) > 1 {
if line[1] == '#' {
Expand Down
6 changes: 5 additions & 1 deletion web/src/utils/remark-plugins/remark-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function isAsciiPunctuation(char: string): boolean {
);
}

function isTagBoundary(char: string): boolean {
return char === "" || /^(?:\p{White_Space}|\p{P}|\p{S})$/u.test(char);
}

/**
* Apply CommonMark backslash-unescaping to a raw source slice, tracking which
* resulting characters came from an escape. `\#` yields a `#` flagged escaped,
Expand Down Expand Up @@ -60,7 +64,7 @@ function parseSegments(chars: string[], escaped: boolean[]): Segment[] {
const prevChar = i > 0 ? chars[i - 1] : "";
const nextChar = chars[i + 1];

if (prevChar === "#" || nextChar === "#" || nextChar === " ") {
if (prevChar === "#" || !isTagBoundary(prevChar) || nextChar === "#" || nextChar === " ") {
segments.push({ type: "text", value: chars[i] });
i++;
continue;
Expand Down
12 changes: 12 additions & 0 deletions web/tests/remark-tag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ describe("remarkTag", () => {
expect(html).toContain('data-tag="memo-tag"');
});

it("requires a boundary before tags", () => {
const html = renderMarkdown('word#tag 1#numeric #standalone (#parentheses) [#brackets] "#quotes" +#symbol');

expect(html).not.toContain('data-tag="tag"');
expect(html).not.toContain('data-tag="numeric"');
expect(html).toContain('data-tag="standalone"');
expect(html).toContain('data-tag="parentheses"');
expect(html).toContain('data-tag="brackets"');
expect(html).toContain('data-tag="quotes"');
expect(html).toContain('data-tag="symbol"');
});

it("does not turn link text or reference link fragments into tags", () => {
const html = renderMarkdown(
[
Expand Down