mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-03 21:56:41 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32cae04627 | ||
|
|
a73e1c6a5b |
@@ -497,6 +497,30 @@ it("recognizes forge references in prose but not in code, links or paths", async
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["见 src/app/main.ts,还有", ["src/app/main.ts"]],
|
||||
["改了 src/a/b/c,然后", ["src/a/b/c"]],
|
||||
["src/foo.ts、src/bar.ts", ["src/foo.ts", "src/bar.ts"]],
|
||||
["查看:docs/说明.md:12。", ["docs/说明.md:12"]],
|
||||
["see src/app/main.ts, ok", ["src/app/main.ts"]],
|
||||
])("recognizes bare file paths next to sentence punctuation: %s", async (source, expected) => {
|
||||
const prepared = prepareMarkdownSource(source, "/repo");
|
||||
const tree = recognizeTextLinks(
|
||||
await parseMarkdown(prepared.render(), { latexMath: false }),
|
||||
prepared.linkRecognition.text,
|
||||
prepared.linkRecognition.inlineCode,
|
||||
);
|
||||
const parts = tree.children?.[0]?.children ?? [];
|
||||
expect(parts.map((node) => node.content ?? node.children?.[0]?.content).join("")).toBe(source);
|
||||
expect(parts.filter((node) => node.type === "Link").map((node) => node.attributes?.url)).toEqual(
|
||||
expected,
|
||||
);
|
||||
expect(toNativeLinkRecognition(prepared.linkRecognition).text).toMatchObject({
|
||||
isDisabled: false,
|
||||
isDefault: false,
|
||||
});
|
||||
});
|
||||
|
||||
describe("bare URL sentence punctuation", () => {
|
||||
it.each([".", ",", ";", ":", "!", "?", ")", ").", "...", "?!"])(
|
||||
"leaves %s outside the link and preserves the rendered sentence",
|
||||
@@ -1227,6 +1251,18 @@ it.each([
|
||||
).toEqual([token]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["更正此前的进度说明:A/B/C/D 主实验", ["A/B/C/D"]],
|
||||
["修改:src/index.ts,然后提交。", ["src/index.ts"]],
|
||||
["见(docs/说明.md)。", ["docs/说明.md"]],
|
||||
["参考「src/index.ts:12」、「README.md」", ["src/index.ts:12", "README.md"]],
|
||||
["“./scripts/run”…", ["./scripts/run"]],
|
||||
["文档在 docs/说明.md 里", ["docs/说明.md"]],
|
||||
])("ends prose paths at CJK and typographic punctuation: %s", (source, expected) => {
|
||||
const { linkRecognition } = prepareMarkdownSource(source, "/repo");
|
||||
expect([...source.matchAll(linkRecognition.text)].map(([text]) => text)).toEqual(expected);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"before/after",
|
||||
"and/or",
|
||||
|
||||
@@ -9,11 +9,56 @@ import { decodeString } from "micromark-util-decode-string";
|
||||
import { fromMarkdown } from "mdast-util-from-markdown";
|
||||
import type { Link, Nodes, Paragraph, PhrasingContent, Root } from "mdast";
|
||||
|
||||
// CJK, fullwidth and typographic punctuation (、。,:()【】「」“”…) ends bare URLs and prose
|
||||
// paths; CJK letters do not.
|
||||
const PUNCTUATION_BOUNDARY_RANGES: readonly (readonly [number, number])[] = [
|
||||
[0x2018, 0x2019],
|
||||
[0x201c, 0x201d],
|
||||
[0x2026, 0x2026],
|
||||
[0x3000, 0x3003],
|
||||
[0x3008, 0x3011],
|
||||
[0x3014, 0x301f],
|
||||
[0xff01, 0xff0f],
|
||||
[0xff1a, 0xff20],
|
||||
[0xff3b, 0xff40],
|
||||
[0xff5b, 0xff65],
|
||||
];
|
||||
|
||||
function formatCharacterRanges(ranges: readonly (readonly [number, number])[]): string {
|
||||
const escape = (codePoint: number) =>
|
||||
`\\u${codePoint.toString(16).toUpperCase().padStart(4, "0")}`;
|
||||
return ranges
|
||||
.map(([start, end]) => (start === end ? escape(start) : `${escape(start)}-${escape(end)}`))
|
||||
.join("");
|
||||
}
|
||||
|
||||
/** The parts of `from`..`to` that no excluded range covers. */
|
||||
function subtractCharacterRanges(
|
||||
from: number,
|
||||
to: number,
|
||||
excluded: readonly (readonly [number, number])[],
|
||||
): [number, number][] {
|
||||
const kept: [number, number][] = [];
|
||||
let next = from;
|
||||
for (const [start, end] of excluded) {
|
||||
if (start > next) kept.push([next, start - 1]);
|
||||
next = end + 1;
|
||||
}
|
||||
if (next <= to) kept.push([next, to]);
|
||||
return kept;
|
||||
}
|
||||
|
||||
const URL_BOUNDARY = formatCharacterRanges(PUNCTUATION_BOUNDARY_RANGES);
|
||||
// Non-ASCII characters a path can contain: everything from U+0080 except boundary punctuation.
|
||||
const PATH_NON_ASCII = formatCharacterRanges(
|
||||
subtractCharacterRanges(0x80, 0xffff, PUNCTUATION_BOUNDARY_RANGES),
|
||||
);
|
||||
|
||||
// Keep this regex in the syntax shared by JavaScript, ICU and java.util.regex.
|
||||
// The Unicode flag makes Metro expand it to escapes rejected by native engines.
|
||||
const PATH_CHARACTER = String.raw`[a-zA-Z0-9\u0080-\uFFFF_@.%+-]`;
|
||||
const PATH_CHARACTER = String.raw`[a-zA-Z0-9${PATH_NON_ASCII}_@.%+-]`;
|
||||
const PATH_LETTER = String.raw`[a-zA-Z]`;
|
||||
const PATH_SEGMENT = String.raw`${PATH_CHARACTER}*[a-zA-Z0-9\u0080-\uFFFF_@%+-]`;
|
||||
const PATH_SEGMENT = String.raw`${PATH_CHARACTER}*[a-zA-Z0-9${PATH_NON_ASCII}_@%+-]`;
|
||||
const LINE_SUFFIX = String.raw`(?::\d+(?::\d+)?(?:-\d+(?::\d+)?)?)?`;
|
||||
const FILE_EXTENSIONS = [...ASSISTANT_FILE_EXTENSIONS]
|
||||
.map((extension) => extension.replace(/[a-z]/g, (letter) => `[${letter}${letter.toUpperCase()}]`))
|
||||
@@ -30,16 +75,13 @@ function createTextPattern(path: string, forgeReferencePattern?: string): RegExp
|
||||
const forgeReference = forgeReferencePattern ? `|${forgeReferencePattern}` : "";
|
||||
// The native adapter mistakes a literal + in lookbehind for a quantifier; use its hex escape.
|
||||
return new RegExp(
|
||||
String.raw`(?:${BARE_URL}${forgeReference}|(?<![a-zA-Z0-9\u0080-\uFFFF_@.%\x2b~:/\\-])(?:${path}|${FILE_NAME})${LINE_SUFFIX}(?![a-zA-Z0-9\u0080-\uFFFF_@%+:/\\-]|\.${PATH_CHARACTER}))`,
|
||||
String.raw`(?:${BARE_URL}${forgeReference}|(?<![a-zA-Z0-9${PATH_NON_ASCII}_@.%\x2b~:/\\-])(?:${path}|${FILE_NAME})${LINE_SUFFIX}(?![a-zA-Z0-9${PATH_NON_ASCII}_@%+:/\\-]|\.${PATH_CHARACTER}))`,
|
||||
"g",
|
||||
);
|
||||
}
|
||||
|
||||
// Matches bare URLs that MD4C does not autolink, like single-label or bracketed hosts.
|
||||
// Quotes and unbalanced closing parentheses end the URL. Trailing sentence punctuation stays outside.
|
||||
// CJK, fullwidth and typographic punctuation (、。,()【】「」“”…) also ends it; CJK letters do not.
|
||||
const URL_BOUNDARY =
|
||||
"\\u2018\\u2019\\u201C\\u201D\\u2026\\u3000-\\u3003\\u3008-\\u3011\\u3014-\\u301F\\uFF01-\\uFF0F\\uFF1A-\\uFF20\\uFF3B-\\uFF40\\uFF5B-\\uFF65";
|
||||
const URL_CHARACTER = String.raw`[^\s<>()"'\x60${URL_BOUNDARY}]`;
|
||||
const URL_GROUP = String.raw`\(${URL_CHARACTER}*\)`;
|
||||
const BARE_URL = String.raw`https?://(?:${URL_CHARACTER}|${URL_GROUP})*(?:[^\s<>()"'\x60.,;:!?${URL_BOUNDARY}]|${URL_GROUP})`;
|
||||
|
||||
@@ -185,9 +185,7 @@ test("filters by search and clears to all plugins", async ({ page }) => {
|
||||
|
||||
await submitSearch(page, "graphite");
|
||||
await expect(page).toHaveURL(/\/plugins\/all\?q=graphite$/);
|
||||
await expect(
|
||||
page.getByRole("heading", { level: 1, name: /^Results for “graphite”/ }),
|
||||
).toBeVisible();
|
||||
await expectSearchResultsAfterDirectory(page, "graphite");
|
||||
await expect(page.getByRole("main").getByRole("link", { name: /Graphite/ })).toBeVisible();
|
||||
await expect(page.getByRole("link", { name: /Dracula/ })).toHaveCount(0);
|
||||
|
||||
@@ -217,10 +215,7 @@ test("submits the visible search term when its input event was missed", async ({
|
||||
await page.keyboard.press("Enter");
|
||||
|
||||
await expect(page).toHaveURL(/\/plugins\/all\?q=graphite&window=month$/);
|
||||
// The URL changes before the results route finishes loading its data and code.
|
||||
await expect(
|
||||
page.getByRole("heading", { level: 1, name: /^Results for “graphite”/ }),
|
||||
).toBeVisible({ timeout: 10000 });
|
||||
await expectSearchResultsAfterDirectory(page, "graphite");
|
||||
await expect(page.getByRole("link", { name: "This month" })).toHaveAttribute(
|
||||
"aria-current",
|
||||
"true",
|
||||
@@ -286,7 +281,7 @@ test("searches from the directory on submit, and Back returns to the directory",
|
||||
|
||||
await page.keyboard.press("Enter");
|
||||
await expect(page).toHaveURL(/\/plugins\/all\?q=gra$/);
|
||||
await expect(page.getByRole("heading", { level: 1, name: /^Results for “gra”/ })).toBeVisible();
|
||||
await expectSearchResultsAfterDirectory(page, "gra");
|
||||
expect(await historyLength(page)).toBe(entries + 1);
|
||||
|
||||
await page.goBack();
|
||||
@@ -501,6 +496,14 @@ async function expectDirectorySearch(page: Page, term: string, window: string) {
|
||||
);
|
||||
}
|
||||
|
||||
// The URL changes as soon as the directory submits, but the results route shows only after
|
||||
// its registry server call returns. On a loaded CI runner that call has taken over 4 s.
|
||||
async function expectSearchResultsAfterDirectory(page: Page, term: string) {
|
||||
await expect(
|
||||
page.getByRole("heading", { level: 1, name: new RegExp(`^Results for “${term}”`) }),
|
||||
).toBeVisible({ timeout: 10000 });
|
||||
}
|
||||
|
||||
async function typeSearch(page: Page, term: string) {
|
||||
await page.getByRole("searchbox", { name: "Search plugins" }).pressSequentially(term);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user