diff --git a/packages/kiritan/src/hash/index.test.ts b/packages/kiritan/src/hash/index.test.ts index 1937025..a7fff73 100644 --- a/packages/kiritan/src/hash/index.test.ts +++ b/packages/kiritan/src/hash/index.test.ts @@ -17,6 +17,23 @@ describe("withHashComment / extractHashComment", () => { expect(extractHashComment(withHash)).toBe("abc123"); }); + it("only matches the marker in the renderer's own comment syntax", () => { + const mdx = (t: string) => `{/* ${t} */}`; + expect(extractHashComment(withHashComment("x", "abc123", mdx), mdx)).toBe( + "abc123" + ); + expect( + extractHashComment(withHashComment("x", "abc123", mdx)) + ).toBeUndefined(); + }); + + it("ignores the same words in prose or a code sample", () => { + expect( + extractHashComment("The kiritan:hash abc123 marker.\n") + ).toBeUndefined(); + expect(extractHashComment("`kiritan:hash abc123`\n")).toBeUndefined(); + }); + it("returns undefined when no hash comment is present", () => { expect(extractHashComment("Just plain content.\n")).toBeUndefined(); }); diff --git a/packages/kiritan/src/hash/index.ts b/packages/kiritan/src/hash/index.ts index 81460c6..05b1c47 100644 --- a/packages/kiritan/src/hash/index.ts +++ b/packages/kiritan/src/hash/index.ts @@ -7,12 +7,26 @@ export function hashText(text: string): string { return createHash("sha1").update(text).digest("hex").slice(0, 16); } -// Matches the marker inside whatever comment syntax the file's renderer wraps it in (`` for Markdown). -const HASH_COMMENT = /kiritan:hash\s+([0-9a-f]+)/; +const defaultComment = (text: string) => ``; -/** Extracts the `kiritan:hash` marker from a `sidecar` output file, if present. */ -export function extractHashComment(text: string): string | undefined { - return HASH_COMMENT.exec(text)?.[1]; +const escapeRegExp = (text: string) => + text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + +/** + * Extracts the `kiritan:hash` marker from a `sidecar` output file, if present. + * The pattern is derived from `comment` (a `Renderer.comment`), so only a marker written the way that renderer writes it matches — the same words in prose or a code sample don't. Whitespace inside the comment is matched loosely. + */ +export function extractHashComment( + text: string, + comment: (text: string) => string = defaultComment +): string | undefined { + const [before, after = ""] = comment("kiritan:hash \u0000").split("\u0000"); + const pattern = new RegExp( + escapeRegExp(before).replace(/ /g, "\\s*") + + "([0-9a-f]+)" + + escapeRegExp(after).replace(/ /g, "\\s*") + ); + return pattern.exec(text)?.[1]; } /** @@ -22,7 +36,7 @@ export function extractHashComment(text: string): string | undefined { export function withHashComment( text: string, hash: string, - comment: (text: string) => string = (t) => `` + comment: (text: string) => string = defaultComment ): string { return `${text.replace(/\n+$/, "")}\n\n${comment(`kiritan:hash ${hash}`)}\n`; } diff --git a/packages/kiritan/src/pipeline/build.test.ts b/packages/kiritan/src/pipeline/build.test.ts index b2e4cf0..65b3d33 100644 --- a/packages/kiritan/src/pipeline/build.test.ts +++ b/packages/kiritan/src/pipeline/build.test.ts @@ -183,6 +183,26 @@ describe("build (sidecar strategy)", () => { expect(ja).toContain("日本語のコンテンツ。"); }); + it("puts the fallback marker after leading front matter, which stays verbatim", async () => { + await writeFile( + join(cwd, "README.base.md"), + "---\ntitle: Hi %{name}\n---\n\n# Kiritan\n", + "utf8" + ); + + await build( + baseConfig({ + sources: [{ glob: "README.base.md", strategy: "sidecar" }], + switcher: { enabled: false }, + }), + { cwd } + ); + + expect(await readFile(join(cwd, "README.ja.md"), "utf8")).toBe( + "---\ntitle: Hi %{name}\n---\n\n\n\n# Kiritan\n" + ); + }); + it("falls back to the base content with a marker when the sidecar file is missing", async () => { await writeFile( join(cwd, "README.base.md"), diff --git a/packages/kiritan/src/pipeline/build.ts b/packages/kiritan/src/pipeline/build.ts index 74430db..a586cf5 100644 --- a/packages/kiritan/src/pipeline/build.ts +++ b/packages/kiritan/src/pipeline/build.ts @@ -90,6 +90,18 @@ function finalizeTree( return renderer.stringify(interpolated); } +// A leading `---` YAML block only counts as front matter as the very first thing in the file, so a marker has to go after it, not before. +const FRONT_MATTER = /^---[ \t]*\r?\n[\s\S]*?\r?\n---[ \t]*(?:\r?\n|$)/; + +function insertAfterFrontMatter(source: string, marker: string): string { + const end = FRONT_MATTER.exec(source)?.[0].length ?? 0; + const head = source.slice(0, end); + const separator = head && !head.endsWith("\n") ? "\n" : ""; + return ( + head + separator + marker + "\n\n" + source.slice(end).replace(/^\r?\n/, "") + ); +} + /** `kiritan build` (docs/DESIGN.md chapter 2): discover -> parse -> resolve -> interpolate -> reassemble -> write. */ export async function build( config: KiritanConfig, @@ -150,7 +162,12 @@ export async function build( text = await readFile(join(cwd, outPath), "utf8"); } catch { text = renderer.comment - ? `${renderer.comment(`kiritan:untranslated (source: ${config.locales.default})`)}\n\n${sourceText}` + ? insertAfterFrontMatter( + sourceText, + renderer.comment( + `kiritan:untranslated (source: ${config.locales.default})` + ) + ) : sourceText; } } diff --git a/packages/kiritan/src/pipeline/check.ts b/packages/kiritan/src/pipeline/check.ts index 23de469..27ab452 100644 --- a/packages/kiritan/src/pipeline/check.ts +++ b/packages/kiritan/src/pipeline/check.ts @@ -94,7 +94,7 @@ async function checkSidecar( // No hash comment means the file predates this feature (or was hand-authored without one) — only flag staleness once a hash comment exists and no longer matches. A renderer with no comment syntax never gets one, so it's never flagged. const existingHash = renderer.comment - ? extractHashComment(outputText) + ? extractHashComment(outputText, renderer.comment) : undefined; if (existingHash && existingHash !== sourceHash) { issues.push({ diff --git a/packages/kiritan/src/pipeline/translate.ts b/packages/kiritan/src/pipeline/translate.ts index 044a692..7aea15d 100644 --- a/packages/kiritan/src/pipeline/translate.ts +++ b/packages/kiritan/src/pipeline/translate.ts @@ -85,7 +85,7 @@ async function translateSidecar( continue; } const existingHash = renderer.comment - ? extractHashComment(existing) + ? extractHashComment(existing, renderer.comment) : undefined; if (existingHash && existingHash !== sourceHash) { staleOrMissingLocales.push(locale);