Skip to content
Merged
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
17 changes: 17 additions & 0 deletions packages/kiritan/src/hash/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
26 changes: 20 additions & 6 deletions packages/kiritan/src/hash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => `<!-- ${text} -->`;

/** 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];
}

/**
Expand All @@ -22,7 +36,7 @@ export function extractHashComment(text: string): string | undefined {
export function withHashComment(
text: string,
hash: string,
comment: (text: string) => string = (t) => `<!-- ${t} -->`
comment: (text: string) => string = defaultComment
): string {
return `${text.replace(/\n+$/, "")}\n\n${comment(`kiritan:hash ${hash}`)}\n`;
}
20 changes: 20 additions & 0 deletions packages/kiritan/src/pipeline/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<!-- kiritan:untranslated (source: en) -->\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"),
Expand Down
19 changes: 18 additions & 1 deletion packages/kiritan/src/pipeline/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kiritan/src/pipeline/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/kiritan/src/pipeline/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading