Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): skip tag markup in link suggestions (#4830)

Andrew Mikofalvy3b2cfe4b5ac9
diff --git a/.changeset/suggest-links-skip-tag-markup.md b/.changeset/suggest-links-skip-tag-markup.md
new file mode 100644
index 000000000..7f3261844
--- /dev/null
+++ b/.changeset/suggest-links-skip-tag-markup.md
@@ -0,0 +1,6 @@
+---
+"@inkeep/open-knowledge": patch
+"@inkeep/open-knowledge-server": patch
+---
+
+Link suggestions no longer count HTML and MDX component tags, such as `<Callout>` or `<Image>`, as mentions of a page with the same name.
diff --git a/knip.config.ts b/knip.config.ts
index 659eef71e..5b8951125 100644
--- a/knip.config.ts
+++ b/knip.config.ts
@@ -143,15 +143,11 @@ export default {
'packages/server': {
entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],
project: 'src/**',
- ignoreDependencies: [
- '@types/shell-quote',
- ],
+ ignoreDependencies: ['@types/shell-quote'],
},
'packages/cli': {
entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],
- ignoreDependencies: [
- 'yjs',
- ],
+ ignoreDependencies: ['yjs'],
},
'packages/desktop': {
entry: [
diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
index a28cc2b18..8a60e5826 100644
--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
@@ -1,4 +1,3 @@
-
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
index 4fc2c11f9..6fcdc23f2 100644
--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { normalizeBridge } from '@inkeep/open-knowledge-core';
import { updateYFragment } from '@tiptap/y-tiptap';
diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.ts
index 9f2f6feb6..40b5cdfd6 100644
--- a/packages/app/tests/integration/interior-edit-freshness.test.ts
+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
index 2a085742b..93666af70 100644
--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.ts
index 878d5196b..867598d34 100644
--- a/packages/app/tests/integration/producer-guard-wiring.test.ts
+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
index 4ada3cf26..0970ac12f 100644
--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterEach, describe, expect, test } from 'vitest';
import { agentWriteMd, createTestServer, type TestServer } from './test-harness';
diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
index cbe2b705f..b632185e1 100644
--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
@@ -1,4 +1,3 @@
-
import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { join } from 'node:path';
diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
index 89bc9b4d9..c530ce4ba 100644
--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';
import type * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.ts
index a78aeb0e4..d3767adc9 100644
--- a/packages/app/tests/integration/undo-after-rollback.test.ts
+++ b/packages/app/tests/integration/undo-after-rollback.test.ts
@@ -1,4 +1,3 @@
-
import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.ts
index e7cc69c3f..b076f955c 100644
--- a/packages/app/tests/integration/undo-recycle-reset.test.ts
+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts
@@ -1,4 +1,3 @@
-
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.ts
index 9ddcd0730..7935e35f8 100644
--- a/packages/app/tests/integration/undo-rederive-survival.test.ts
+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter } from '@inkeep/open-knowledge-core';
import { Plugin, PluginKey } from '@tiptap/pm/state';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.ts
index 2a597ec3d..f4cc8a286 100644
--- a/packages/core/src/bridge/compose.ts
+++ b/packages/core/src/bridge/compose.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';
export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';
diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.ts
index 01ef76a9f..8fb428b90 100644
--- a/packages/core/src/bridge/doc-boundary-space.ts
+++ b/packages/core/src/bridge/doc-boundary-space.ts
@@ -1,4 +1,3 @@
-
import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';
import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';
diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.ts
index fb0ba5a7c..fc267cbea 100644
--- a/packages/core/src/markdown/comment-promoter.ts
+++ b/packages/core/src/markdown/comment-promoter.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.ts
index fa3cfcd86..bfd7e3109 100644
--- a/packages/core/src/markdown/merged-walker.ts
+++ b/packages/core/src/markdown/merged-walker.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Parent, Root } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.ts
index 8be632ba4..4d3250d3a 100644
--- a/packages/core/src/markdown/safe-url.ts
+++ b/packages/core/src/markdown/safe-url.ts
@@ -1,4 +1,3 @@
-
export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;
const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');
diff --git a/packages/server/src/suggest-links.test.ts b/packages/server/src/suggest-links.test.ts
index 009cb696f..10fcf9ea0 100644
--- a/packages/server/src/suggest-links.test.ts
+++ b/packages/server/src/suggest-links.test.ts
@@ -158,6 +158,75 @@ describe('suggestLinks', () => {
}
});
+ test('ignores HTML and JSX tag markup but keeps text between tags matchable', async () => {
+ const projectDir = mkdtempSync(join(tmpdir(), 'ok-suggest-links-'));
+ const contentDir = join(projectDir, 'content');
+ mkdirSync(contentDir, { recursive: true });
+ const hocuspocus = new Hocuspocus({ quiet: true });
+ const source = [
+ '<Callout type="warn">',
+ 'Always use a Callout for warnings.',
+ '</Callout>',
+ '',
+ '<Image src="/callout.png" alt="Callout" />',
+ '',
+ 'Mark it <b>Callout</b> in bold.',
+ '',
+ '<!-- Callout is disabled here -->',
+ ].join('\n');
+
+ try {
+ writeFileSync(join(contentDir, 'callout.md'), '# Callout\n', 'utf-8');
+ writeFileSync(join(contentDir, 'notes.md'), source, 'utf-8');
+
+ const result = await suggestLinks({
+ hocuspocus,
+ fileIndex: buildFileIndex(contentDir, ['callout', 'notes']),
+ docName: 'callout',
+ });
+
+ expect(result.mentions).toEqual([
+ {
+ source: 'notes',
+ excerpt: 'Always use a Callout for warnings.',
+ offset: source.indexOf('Callout for warnings'),
+ },
+ {
+ source: 'notes',
+ excerpt: 'Mark it <b>Callout</b> in bold.',
+ offset: source.indexOf('<b>Callout') + '<b>'.length,
+ },
+ ]);
+ } finally {
+ rmSync(projectDir, { recursive: true, force: true });
+ }
+ });
+
+ test('keeps prose around a stray angle bracket matchable', async () => {
+ const projectDir = mkdtempSync(join(tmpdir(), 'ok-suggest-links-'));
+ const contentDir = join(projectDir, 'content');
+ mkdirSync(contentDir, { recursive: true });
+ const hocuspocus = new Hocuspocus({ quiet: true });
+ const source = 'When a<b holds, prefer the Callout pattern over x>y checks.';
+
+ try {
+ writeFileSync(join(contentDir, 'callout.md'), '# Callout\n', 'utf-8');
+ writeFileSync(join(contentDir, 'notes.md'), source, 'utf-8');
+
+ const result = await suggestLinks({
+ hocuspocus,
+ fileIndex: buildFileIndex(contentDir, ['callout', 'notes']),
+ docName: 'callout',
+ });
+
+ expect(result.mentions).toEqual([
+ { source: 'notes', excerpt: source, offset: source.indexOf('Callout') },
+ ]);
+ } finally {
+ rmSync(projectDir, { recursive: true, force: true });
+ }
+ });
+
test('keeps labels linked to other pages matchable', async () => {
const projectDir = mkdtempSync(join(tmpdir(), 'ok-suggest-links-'));
const contentDir = join(projectDir, 'content');
diff --git a/packages/server/src/suggest-links.ts b/packages/server/src/suggest-links.ts
index 9465b0da6..a5358d762 100644
--- a/packages/server/src/suggest-links.ts
+++ b/packages/server/src/suggest-links.ts
@@ -153,6 +153,41 @@ function readInlineCode(line: string, start: number): { text: string; nextIndex:
return { text: line.slice(start, openEnd), nextIndex: openEnd };
}
+const HTML_TAG_HEAD_RE = /<\/?[A-Za-z][\w.-]*(?=[\s/>])/y;
+const HTML_TAG_BODY_CHAR_RE = /[\s\w.:@$=/-]/;
+
+function readHtmlTag(line: string, start: number): { text: string; nextIndex: number } | null {
+ if (line.startsWith('<!--', start)) {
+ const close = line.indexOf('-->', start + 4);
+ return close === -1 ? null : { text: line.slice(start, close + 3), nextIndex: close + 3 };
+ }
+
+ HTML_TAG_HEAD_RE.lastIndex = start;
+ const head = HTML_TAG_HEAD_RE.exec(line);
+ if (!head) return null;
+
+ let quote: string | null = null;
+ let braceDepth = 0;
+ for (let index = start + head[0].length; index < line.length; index += 1) {
+ const char = line[index];
+ if (quote) {
+ if (char === quote) quote = null;
+ } else if (char === '"' || char === "'" || char === '`') {
+ quote = char;
+ } else if (char === '{') {
+ braceDepth += 1;
+ } else if (char === '}') {
+ braceDepth -= 1;
+ } else if (braceDepth === 0 && char === '>') {
+ return { text: line.slice(start, index + 1), nextIndex: index + 1 };
+ } else if (braceDepth === 0 && !HTML_TAG_BODY_CHAR_RE.test(char)) {
+ return null;
+ }
+ }
+
+ return null;
+}
+
function readWikiLink(
line: string,
start: number,
@@ -349,6 +384,15 @@ function scanLineForMentions(
}
}
+ if (line[index] === '<') {
+ const tag = readHtmlTag(line, index);
+ if (tag) {
+ appendNonMatchableText(tag.text);
+ index = tag.nextIndex;
+ continue;
+ }
+ }
+
if (line[index] === '!' && line[index + 1] === '[') {
const image = readMarkdownImage(line, index);
if (image) {