Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(documents): preserve embedded fences in code imports

Afloatfe803d899ff5
diff --git a/apps/node-services/AGENTS.md b/apps/node-services/AGENTS.md
index 1640db74..070d93c7 100644
--- a/apps/node-services/AGENTS.md
+++ b/apps/node-services/AGENTS.md
@@ -43,6 +43,13 @@
and the host passes it to the agent role only when both run in the same
process; the Agent calls it directly, not over HTTP.
+## Text document conversion
+
+Imported code, JSON, XML and YAML remain literal code-block content in the
+normalized Markdown. Embedded backtick runs must not close the surrounding
+fence, and filename-derived language metadata must not introduce backticks or
+line breaks into its opening line.
+
## Configuration and Paths
- Roles read only the process environment. Package scripts load the root `.env`
diff --git a/apps/node-services/src/document-normalizer/document-normalizer-core.ts b/apps/node-services/src/document-normalizer/document-normalizer-core.ts
index 10f3bf77..9ef48876 100644
--- a/apps/node-services/src/document-normalizer/document-normalizer-core.ts
+++ b/apps/node-services/src/document-normalizer/document-normalizer-core.ts
@@ -311,5 +311,11 @@ function jsonToMarkdown(text: string): string {
function fencedBlock(language: string, body: string): string {
const normalized = body.replace(/^\uFEFF/u, "").replace(/\n+$/u, "");
- return `\`\`\`${language}\n${normalized}\n\`\`\``;
+ let fenceLength = 3;
+ for (const run of normalized.matchAll(/`+/gu)) {
+ fenceLength = Math.max(fenceLength, run[0].length + 1);
+ }
+ const fence = "`".repeat(fenceLength);
+ const info = /^[^`\r\n]*$/u.test(language) ? language : "text";
+ return `${fence}${info}\n${normalized}\n${fence}`;
}
diff --git a/apps/node-services/tests/document-normalizer/document-code-fences.test.ts b/apps/node-services/tests/document-normalizer/document-code-fences.test.ts
new file mode 100644
index 00000000..5a3eed28
--- /dev/null
+++ b/apps/node-services/tests/document-normalizer/document-code-fences.test.ts
@@ -0,0 +1,72 @@
+import { createHash } from "node:crypto";
+
+import { extractDocumentImages } from "@linger/document-files";
+import { describe, expect, it } from "vitest";
+
+import { normalizeDocumentContent } from "../../src/document-normalizer/document-normalizer-core.js";
+
+const encode = (value: string): Uint8Array => new TextEncoder().encode(value);
+const decode = (value: Uint8Array): string => new TextDecoder().decode(value);
+
+describe("fenced document source content", () => {
+ it.each([
+ ["example.py", "text/x-python", "python", "```"],
+ ["example.yaml", "application/yaml", "yaml", "````"],
+ ["example.xml", "application/xml", "xml", "`````"],
+ ])(
+ "keeps a source fence inside the code block: %s",
+ async (filename, mediaType, language, embeddedFence) => {
+ const source = `before\n${embeddedFence}\n# Literal heading\n${embeddedFence}\nafter`;
+ const content = await normalizeDocumentContent({
+ filename,
+ mediaType,
+ source: encode(source),
+ });
+ const fence = "`".repeat(embeddedFence.length + 1);
+ expect(decode(content)).toBe(
+ `${fence}${language}\n${source}\n${fence}\n`,
+ );
+ },
+ );
+
+ it("keeps image syntax in imported code out of image extraction", async () => {
+ const source =
+ 'example = """\n```\n![Diagram](data:image/gif;base64,R0lGODlh)\n```\n"""';
+ const content = await normalizeDocumentContent({
+ filename: "example.py",
+ mediaType: "text/x-python",
+ source: encode(source),
+ });
+ const result = extractDocumentImages({
+ filePath: "example.md",
+ content,
+ sha256: createHash("sha256").update(content).digest("hex"),
+ sizeBytes: content.byteLength,
+ });
+ expect(result.files.size).toBe(0);
+ expect(decode(result.content)).toContain(source);
+ });
+
+ it.each(["example.custom`type", "example.custom\ntype"])(
+ "keeps an invalid fence language from changing the block boundary: %s",
+ async (filename) => {
+ await expect(
+ normalizeDocumentContent({
+ filename,
+ mediaType: "text/javascript",
+ source: encode("const value = 1;"),
+ }).then(decode),
+ ).resolves.toBe("```text\nconst value = 1;\n```\n");
+ },
+ );
+
+ it("keeps ordinary code fences unchanged", async () => {
+ await expect(
+ normalizeDocumentContent({
+ filename: "example.py",
+ mediaType: "text/x-python",
+ source: encode("print('hello')\n"),
+ }).then(decode),
+ ).resolves.toBe("```python\nprint('hello')\n```\n");
+ });
+});