diff --git a/.gitignore b/.gitignore index b24d71e..46f0184 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ node_modules/ dist/ + # Compiled Java class files *.class @@ -28,6 +29,7 @@ dist/ # Unit test reports TEST*.xml +coverage/ # Generated by MacOS .DS_Store diff --git a/src/ast-to-html.mjs b/src/ast-to-html.mjs new file mode 100644 index 0000000..54aa4e9 --- /dev/null +++ b/src/ast-to-html.mjs @@ -0,0 +1,7 @@ +import { toHtml } from 'hast-util-to-html'; + +export function htmlToAst(ast) { + return toHtml(ast); +} + +export default htmlToAst; diff --git a/src/interpolate.mjs b/src/interpolate.mjs index 1fd94b4..5b517bb 100644 --- a/src/interpolate.mjs +++ b/src/interpolate.mjs @@ -28,7 +28,6 @@ function getMaskedData(node, ancestors) { function interpolateNode(node, ancestors) { const data = getMaskedData(node, ancestors); - // Interpolate properties if (node.properties && typeof node.properties === 'object') { for (const key in node.properties) { if (typeof node.properties[key] === 'string') { @@ -44,7 +43,6 @@ function interpolateNode(node, ancestors) { } } - // Interpolate text content for text nodes if (node.type === 'text' && node.value) { node.value = interpolateString(node.value, data.interp); } @@ -54,9 +52,14 @@ function interpolateNode(node, ancestors) { function interpolateString(str, data) { const context = createContext({ ...data, ...global }); - const interpolatedStr = runInContext(`\`${str}\``, context); - return interpolatedStr; + try { + const interpolatedStr = runInContext(`\`${str}\``, context); + return interpolatedStr; + } catch (e) { + console.error('Error interpolating string:', e); + return str; + } } export default interpolate; diff --git a/src/util.mjs b/src/util.mjs index 8b13789..d5848d5 100644 --- a/src/util.mjs +++ b/src/util.mjs @@ -1 +1,20 @@ +export function cloneNode (node) { + return JSON.parse(JSON.stringify(node)); +} +export function createAstNode (type, props, children) { + const ast = { + type: type, + props: props, + children: children || [], + }; + return ast; +} + +// Test if the node is an object with a "type" value (including undefined) +export function isAstNode (node) { + return (typeof node === 'object') + && (node !== null) + && ('type' in node) + ; +} diff --git a/test/interpolate.test.mjs b/test/interpolate.test.mjs index 0d6b4d2..19ab321 100644 --- a/test/interpolate.test.mjs +++ b/test/interpolate.test.mjs @@ -8,8 +8,7 @@ describe("interpolate()", () => { expect(interpolate).toBeDefined(); }) - it("should interpolate", () => { - + it("should interpolate a single node", () => { const uninterpolated = '
'; const expected = ''; @@ -21,4 +20,69 @@ describe("interpolate()", () => { expect(rendered).toEqual(expected); }) + it("should interpolate a nested tree", () => { + const uninterpolated = ` +This is a \${data.stringInPTag} in a p tag
+This is a p with an interpolated id
+This is a string in a p tag
+This is a p with an interpolated id
+ +simple: 3 * 12 = \${3 * 12}
+named function: 3^2 = \${square(3)}
+arrow function: 5*2 = \${(num => (num * 2))(5)}
+arrow function: 3-8 = \${(num => (num - 8))(3)}
+simple: 3 * 12 = 36
+named function: 3^2 = 9
+arrow function: 5*2 = 10
+arrow function: 3-8 = -5
+