Skip to content

Commit 6388f4a

Browse files
committed
ts pandoc codegen - pandocNativeStr
1 parent cdb4a7e commit 6388f4a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/core/pandoc/codegen.ts

+17
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ export function pandocBlock(delimiterCharacter: ":" | "`") {
232232
};
233233
}
234234

235+
export function pandocNativeStr(content: string): PandocNode {
236+
return {
237+
...basePandocNode,
238+
emit: (ls: EitherString[]) => {
239+
const maxBackticks = content.match(/`+/g)?.[0].length || 0;
240+
const backticks = "`".repeat(maxBackticks + 1);
241+
const escapedContent = content.replaceAll(
242+
'"',
243+
'\\"',
244+
);
245+
ls.push(
246+
`${backticks}${'Str "'}${escapedContent}${'"'}${backticks}{=pandoc-native}`,
247+
);
248+
},
249+
};
250+
}
251+
235252
export const pandocDiv = pandocBlock(":");
236253
export const pandocCode = pandocBlock("`");
237254
export const pandocFigure = pandocHtmlBlock("figure");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* native-string.test.ts
3+
*
4+
* Copyright (C) 2021-2022 Posit Software, PBC
5+
*
6+
*/
7+
8+
import bounds from "binary-search-bounds";
9+
10+
import { unitTest } from "../../test.ts";
11+
import { assert } from "testing/asserts.ts";
12+
import { pandocNativeStr } from "../../../src/core/pandoc/codegen.ts";
13+
14+
// deno-lint-ignore require-await
15+
unitTest("native-string - basics", async () => {
16+
assert(pandocNativeStr("hello").mappedString().value === '`Str "hello"`{=pandoc-native}');
17+
assert(pandocNativeStr('"hello"').mappedString().value === '`Str "\\"hello\\""`{=pandoc-native}');
18+
assert(pandocNativeStr('"hel`lo"').mappedString().value === '``Str "\\"hel`lo\\""``{=pandoc-native}');
19+
})

0 commit comments

Comments
 (0)