From 2f7f701057539c18364d44174d09ce58f9f65450 Mon Sep 17 00:00:00 2001 From: arunkumar Date: Wed, 24 Feb 2021 01:25:36 +0530 Subject: [PATCH] AST to Markdown function and its test cases added --- lib/compileASTtoMarkdown.js | 22 ++++++++++++++ lib/index.js | 3 +- test/AST_To_Markdown.test.js | 72 ++++++++++++++++++++++++++++++++++++++++++++ test/index.test.js | 7 +---- 4 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 lib/compileASTtoMarkdown.js create mode 100644 test/AST_To_Markdown.test.js diff --git a/lib/compileASTtoMarkdown.js b/lib/compileASTtoMarkdown.js new file mode 100644 index 0000000..d1e7c50 --- /dev/null +++ b/lib/compileASTtoMarkdown.js @@ -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; diff --git a/lib/index.js b/lib/index.js index e109556..f90b1cd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,8 +13,7 @@ function* prepare(str) { function Parse(str, inner = false) { let context = null; let buffer = ""; - let AST = []; - let link = ""; + let AST = []; let link = ""; const flush = () => { if (buffer) { diff --git a/test/AST_To_Markdown.test.js b/test/AST_To_Markdown.test.js new file mode 100644 index 0000000..114f2e9 --- /dev/null +++ b/test/AST_To_Markdown.test.js @@ -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**", + ); + }); +}); diff --git a/test/index.test.js b/test/index.test.js index 840607e..dfde6f1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -60,12 +60,7 @@ describe("Parser Checks", function () { expect( Parse( "**Bold_Italics~~strikethrough==underline==strikethrough~~Italics_Bold**", - ),[ - { - type: 'underline', - children: [ 'The', { type: 'bold', children: [ 'KING' ] }, 'Maker' ] - } - ] + ), ).to.eql([ { type: "bold",