Skip to content

Commit ab9f883

Browse files
committed
feat: inline code placeholders
1 parent d6fb738 commit ab9f883

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

.changeset/fluffy-ties-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
inline code placeholders

packages/cli/src/cli/loaders/mdx2/code-placeholder.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,36 @@ describe("MDX Code Placeholder Loader", () => {
276276
expect(pushed).toBe(md);
277277
});
278278
});
279+
280+
describe("inline code placeholder", () => {
281+
it("should replace inline code with placeholder on pull", async () => {
282+
const md = "This is some `inline()` code.";
283+
const pulled = await loader.pull("en", md);
284+
const hash = md5("`inline()`");
285+
const expected = `This is some ---INLINE_CODE_PLACEHOLDER_${hash}--- code.`;
286+
expect(pulled).toBe(expected);
287+
});
288+
289+
it("should restore inline code from placeholder on push", async () => {
290+
const md = "Some `code` here.";
291+
const pulled = await loader.pull("en", md);
292+
const translated = pulled.replace("Some", "Algún");
293+
const pushed = await loader.push("es", translated);
294+
expect(pushed).toBe("Algún `code` here.");
295+
});
296+
297+
it("round-trips multiple inline code snippets", async () => {
298+
const md = "Use `a` and `b` and `c`.";
299+
const pulled = await loader.pull("en", md);
300+
const pushed = await loader.push("es", pulled);
301+
expect(pushed).toBe(md);
302+
});
303+
304+
it("handles identical inline snippets correctly", async () => {
305+
const md = "Repeat `x` and `x` again.";
306+
const pulled = await loader.pull("en", md);
307+
const pushed = await loader.push("es", pulled);
308+
expect(pushed).toBe(md);
309+
});
310+
});
279311
});

packages/cli/src/cli/loaders/mdx2/code-placeholder.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { md5 } from "../../utils/md5";
44
import _ from "lodash";
55

66
const fenceRegex = /([ \t]*)(^>\s*)?```([\s\S]*?)```/gm;
7+
const inlineCodeRegex = /(?<!`)`([^`\r\n]+?)`(?!`)/g;
78

89
function ensureTrailingFenceNewline(_content: string) {
910
let found = false;
@@ -49,7 +50,6 @@ function extractCodePlaceholders(content: string): {
4950
const codePlaceholders: Record<string, string> = {};
5051

5152
const codeBlockMatches = finalContent.matchAll(fenceRegex);
52-
5353
for (const match of codeBlockMatches) {
5454
const codeBlock = match[0];
5555
const codeBlockHash = md5(codeBlock);
@@ -63,6 +63,18 @@ function extractCodePlaceholders(content: string): {
6363
finalContent = finalContent.replace(codeBlock, replacement);
6464
}
6565

66+
const inlineCodeMatches = finalContent.matchAll(inlineCodeRegex);
67+
for (const match of inlineCodeMatches) {
68+
const inlineCode = match[0];
69+
const inlineCodeHash = md5(inlineCode);
70+
const placeholder = `---INLINE_CODE_PLACEHOLDER_${inlineCodeHash}---`;
71+
72+
codePlaceholders[placeholder] = inlineCode;
73+
74+
const replacement = placeholder;
75+
finalContent = finalContent.replace(inlineCode, replacement);
76+
}
77+
6678
return {
6779
content: finalContent,
6880
codePlaceholders,

0 commit comments

Comments
 (0)