4 changed files with 96 additions and 8 deletions
@ -0,0 +1,22 @@ |
|||||
|
const { inspect } = require("util"); |
||||
|
const nodeTypes = { |
||||
|
bold: "**", |
||||
|
italics: "_", |
||||
|
strikethrough: "~~", |
||||
|
underline: "==", |
||||
|
text: "", |
||||
|
}; |
||||
|
|
||||
|
const CompileASTtoMarkdown = ast => |
||||
|
ast.reduce((acc, value) => { |
||||
|
if (typeof value === "string") return acc + value; |
||||
|
if (value.type === "url") { |
||||
|
return acc + `[${CompileASTtoMarkdown(value.children)}](${value.url})`; |
||||
|
} |
||||
|
let str = nodeTypes[value.type]; |
||||
|
str += CompileASTtoMarkdown(value.children); |
||||
|
str += nodeTypes[value.type]; |
||||
|
return acc + str; |
||||
|
}, ""); |
||||
|
|
||||
|
module.exports = CompileASTtoMarkdown; |
@ -0,0 +1,72 @@ |
|||||
|
const expect = require("chai").expect; |
||||
|
const Compile = require("../lib/compileASTtoMarkdown"); |
||||
|
|
||||
|
describe("Compiling AST to Markdown", function () { |
||||
|
it("sample ast - 1", function () { |
||||
|
const input = [{ type: "bold", children: ["bold"] }]; |
||||
|
expect(Compile(input)).to.equal("**bold**"); |
||||
|
}); |
||||
|
|
||||
|
it("sample ast - 2", function () { |
||||
|
const input = [ |
||||
|
{ type: "bold", children: ["my"] }, |
||||
|
{ type: "text", children: ["simple"] }, |
||||
|
{ type: "italics", children: ["AST"] }, |
||||
|
]; |
||||
|
expect(Compile(input)).to.equal("**my**simple_AST_"); |
||||
|
}); |
||||
|
|
||||
|
it("sentence ast", function () { |
||||
|
const input = [ |
||||
|
{ type: "bold", children: ["Arunkumar"] }, |
||||
|
{ type: "text", children: [" is the "] }, |
||||
|
{ type: "underline", children: ["Best Boy"] }, |
||||
|
{ type: "text", children: [" not the "] }, |
||||
|
{ |
||||
|
type: "italics", |
||||
|
children: [{ type: "strikethrough", children: ["Worst Boy"] }], |
||||
|
}, |
||||
|
{ type: "text", children: ["."] }, |
||||
|
]; |
||||
|
expect(Compile(input)).to.equal( |
||||
|
"**Arunkumar** is the ==Best Boy== not the _~~Worst Boy~~_.", |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
it("link ast", function () { |
||||
|
const input = [ |
||||
|
{ type: "url", children: ["The Feathers"], url: "feathers.studio" }, |
||||
|
]; |
||||
|
expect(Compile(input)).to.equal("[The Feathers](feathers.studio)"); |
||||
|
}); |
||||
|
|
||||
|
it("Nested ast", function () { |
||||
|
const input = [ |
||||
|
{ |
||||
|
type: "bold", |
||||
|
children: [ |
||||
|
"Bold", |
||||
|
{ |
||||
|
type: "italics", |
||||
|
children: [ |
||||
|
"Italics", |
||||
|
{ |
||||
|
type: "strikethrough", |
||||
|
children: [ |
||||
|
"Strikethrough", |
||||
|
{ type: "underline", children: ["Underline"] }, |
||||
|
"Strikethrough", |
||||
|
], |
||||
|
}, |
||||
|
"Italics", |
||||
|
], |
||||
|
}, |
||||
|
"Bold", |
||||
|
], |
||||
|
}, |
||||
|
]; |
||||
|
expect(Compile(input)).to.equal( |
||||
|
"**Bold_Italics~~Strikethrough==Underline==Strikethrough~~Italics_Bold**", |
||||
|
); |
||||
|
}); |
||||
|
}); |
Loading…
Reference in new issue