diff --git a/README.md b/README.md index 583211d..da58e53 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,13 @@ This lib parses markdown into a simplified Abstract Syntax tree. Several Note-Taking apps are 'node-based,' 'markdown based,' etc. -I use Markdown, and I cannot get used to outlines. However, I recognize the benefits of having node-based systems, where you can reference a specific node from any other node. +I use Markdown, and I cannot get used to outlines. However, I recognize the benefits of having node-based systems, where +you can reference a specific node from any other node. So my question was: How can I have these nodes, and still use Markdown? -After considering this, I realized that Markdown has some structure. It has headers that can be inside other headings and lists that can be inside other lists. These are the nodes this library generates. +After considering this, I realized that Markdown has some structure. It has headers that can be inside other headings +and lists that can be inside other lists. These are the nodes this library generates. ## Usage @@ -20,7 +22,7 @@ Say you have the following markdown --- hello: world --- - + # Heading 1 Some text under Heading 1 @@ -29,8 +31,8 @@ Text that has (inline::variables) ## Inline elements -- Tana and logseq likes - - embedded nodes +- Tana and logseq likes + - embedded nodes ``` The lib @@ -105,6 +107,12 @@ will produce the following Json ``` +## Based on + +- [remark](https://github.com/remarkjs/remark) +- [unifiedjs](https://github.com/unifiedjs/unified) + ## And then? -I use this structure to later produce RDF, but you can use it for whatever you want. +I use this structure to later produce [RDF](https://en.wikipedia.org/wiki/Resource_Description_Framework) using +a [vault-triplifier](https://github.com/cristianvasquez/vault-triplifier), but you can use it for whatever you want. diff --git a/example/example.js b/example/example.js new file mode 100644 index 0000000..a149e9e --- /dev/null +++ b/example/example.js @@ -0,0 +1,20 @@ +import { simpleAst } from '../index.js' + +const markdown = `--- +hello: world +--- + +# Heading 1 + +Some text under Heading 1 + +Text that has (inline::variables) + +## Inline elements + +- Tana and logseq likes + - embedded nodes` + +const ast = simpleAst(markdown, { normalize: true, includePosition: true }) + +console.log(JSON.stringify(ast, null, 2)) diff --git a/index.js b/index.js index aaa516e..c6036c7 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,14 @@ import { simpleAst as sa } from './src/simpleAst.js' import { createMarkdownParser } from './src/markdown/markdownParser.js' -function simpleAst ( - fullText, options = { normalize: false, inlineAsArray: false }) { +const defaultOptions = { + normalize: false, inlineAsArray: false, includePosition: false, +} + +function simpleAst (fullText, options = {}) { const parser = createMarkdownParser() const astNode = parser.parse(fullText) - return sa({ astNode, fullText }, options) + return sa({ astNode, fullText }, { ...defaultOptions, ...options }) } export { simpleAst } diff --git a/playground/readme.md b/playground/readme.md index e69de29..ace1a1b 100644 --- a/playground/readme.md +++ b/playground/readme.md @@ -0,0 +1,13 @@ +# Playground + +To run + +```sh +npm run install +``` + +Then + +```sh +npm run dev +``` diff --git a/src/simpleAst.js b/src/simpleAst.js index 6de3221..fa04b5a 100644 --- a/src/simpleAst.js +++ b/src/simpleAst.js @@ -5,13 +5,11 @@ import { annotateInlineFields, annotateTags, annotateYAML, annotateBlockIds, } from './defaultAnnotator.js' -const DEFAULT_OPTIONS = { - normalize: true, -} - -function simpleAst ({ astNode, fullText }, options = {}) { +function simpleAst ({ astNode, fullText }, options) { - const _options = { ...DEFAULT_OPTIONS, ...options } + if (!options) { + throw Error('Requires options') + } const root = { type: 'root', depth: 0, @@ -22,9 +20,9 @@ function simpleAst ({ astNode, fullText }, options = {}) { if (astNode.type === 'yaml') { return annotateYAML({ value: astNode.value, currentNode: current }, - _options) + options) } else if (astNode.type === 'code') { - const block = createBlock({ astNode, fullText, type: 'code' }, _options) + const block = createBlock({ astNode, fullText, type: 'code' }, options) block.lang = astNode.lang if (astNode.meta) { block.meta = astNode.meta @@ -39,17 +37,22 @@ function simpleAst ({ astNode, fullText }, options = {}) { ? ancesters[ancesters.length - 1] : current - const block = createBlock({ astNode, fullText, type: 'block' }, _options) + const block = createBlock({ astNode, fullText, type: 'block' }, options) block.depth = astNode.depth + + if (options.includePosition && astNode.position) { + block.position = astNode.position + } + push(parent, block) headersStack.push(block) return block } else if (astNode.type === 'list') { const outline = getOutline({ astNode, fullText, outlineDepth: 0 }, - _options) + options) push(current, outline) } else if (astNode.type === 'paragraph') { - const block = createBlock({ astNode, fullText, type: 'text' }, _options) + const block = createBlock({ astNode, fullText, type: 'text' }, options) push(current, block) } else { // Things not yet handled @@ -89,9 +92,16 @@ function getOutline ({ astNode, fullText, outlineDepth, depth }, options) { } } } - return { + + const block = { type: 'outline', ordered: astNode.ordered, children, } + + if (options.includePosition && astNode.position) { + block.position = astNode.position + } + + return block } function createBlock ({ astNode, fullText, type }, options) { diff --git a/test/__snapshots__/simpleAST.js.snap b/test/__snapshots__/simpleAST.js.snap index acb77d1..b86b289 100644 --- a/test/__snapshots__/simpleAST.js.snap +++ b/test/__snapshots__/simpleAST.js.snap @@ -10,7 +10,7 @@ Object { Object { "data": Array [ Object { - "description": "Section 1", + "description": " Section 1", }, ], "links": Array [ @@ -21,22 +21,23 @@ Object { }, ], "type": "text", - "value": "And some more [[Bob Details#^2]]", + "value": "description:: Section 1 +And some more [[Bob Details#^2]]", }, ], "depth": 2, "ids": Array [ - "1", + "^1", ], "type": "block", - "value": "Section", + "value": "## Section ^1", }, Object { "children": Array [ Object { "data": Array [ Object { - "description": "Section 2", + "description": " Section 2", }, ], "links": Array [ @@ -47,22 +48,23 @@ Object { }, ], "type": "text", - "value": "And some more [[Bob Details#^3 | Pointer to section 3]]", + "value": "description:: Section 2 +And some more [[Bob Details#^3 | Pointer to section 3]]", }, ], "depth": 2, "ids": Array [ - "2", + "^2", ], "type": "block", - "value": "Section", + "value": "## Section ^2", }, Object { "children": Array [ Object { "data": Array [ Object { - "description": "Section 3", + "description": " Section 3", }, ], "links": Array [ @@ -73,39 +75,40 @@ Object { }, ], "type": "text", - "value": "And some more [[Unknown#^1]]", + "value": "description:: Section 3 +And some more [[Unknown#^1]]", }, ], "depth": 2, "ids": Array [ - "3", + "^3", ], "type": "block", - "value": "Section", + "value": "## Section ^3", }, Object { "children": Array [ Object { "data": Array [ Object { - "description": "Section 4", + "description": " Section 4", }, ], "type": "text", - "value": "", + "value": "description:: Section 4", }, ], "depth": 2, "tags": Array [ - "Section", + "#Section", ], "type": "block", - "value": "Section", + "value": "## Section #Section", }, ], "depth": 1, "type": "block", - "value": "Details about Bob", + "value": "# Details about Bob", }, ], "depth": 0, @@ -119,82 +122,82 @@ Object { Object { "data": Array [ Object { - "inline": "field", + "inline ": " field", }, ], "type": "text", - "value": "", + "value": "inline :: field", }, Object { "data": Array [ Object { - "inline": "field", + " inline ": " field ", }, ], "type": "text", - "value": "nested", + "value": "nested ( inline :: field )", }, Object { "data": Array [ Object { - "**Thoughts**": "It was decent.", + "**Thoughts**": " It was decent.", }, ], "type": "text", - "value": "", + "value": "**Thoughts**:: It was decent.", }, Object { "data": Array [ Object { - "**Rating**": "6'", + "**Rating**": " 6'", }, ], "type": "text", - "value": "", + "value": "**Rating**:: 6'", }, Object { "data": Array [ Object { - "subject": Object { - "inline": "field", + "subject ": Object { + " inline ": " field", }, }, ], "type": "text", - "value": "", + "value": "subject :: inline :: field", }, Object { "data": Array [ Object { - "subject": Object { - "inline": "field", + " subject ": Object { + " inline ": " field ", }, }, ], "type": "text", - "value": "nested", + "value": "nested ( subject :: inline :: field )", }, Object { "data": Array [ Object { - "subject": Object { - "**Thoughts**": "It was decent.", + "subject ": Object { + " **Thoughts**": " It was decent.", }, }, ], "type": "text", - "value": "", + "value": "subject :: **Thoughts**:: It was decent.", }, Object { "data": Array [ Object { - "subject": Object { - "**Rating**": "6", + "subject ": Object { + " **Rating**": " 6", }, }, ], "type": "text", - "value": "", + "value": "subject :: **Rating**:: 6", }, Object { "type": "text", @@ -203,21 +206,21 @@ Object { Object { "data": Array [ Object { - "Too": Object { - "many": Object { - "inline": "fields", + "Too ": Object { + " many ": Object { + " inline ": " fields", }, }, }, ], "type": "text", - "value": "", + "value": "Too :: many :: inline :: fields", }, Object { "data": Array [ Object { - "[[Bob]]": Object { - "foaf:knows": "[[Alice]]", + "[[Bob]] ": Object { + " foaf:knows ": " [[Alice]]", }, }, ], @@ -234,7 +237,7 @@ Object { }, ], "type": "text", - "value": "", + "value": "[[Bob]] :: foaf:knows :: [[Alice]]", }, ], "depth": 0, @@ -248,7 +251,7 @@ Object { Object { "data": Array [ Object { - "has image": "![Lovely image](../houses/img.png)", + "has image ": " ![Lovely image](../houses/img.png)", }, ], "links": Array [ @@ -258,7 +261,7 @@ Object { }, ], "type": "text", - "value": "", + "value": "has image :: ![Lovely image](../houses/img.png)", }, Object { "links": Array [ @@ -369,7 +372,7 @@ Object { }, ], "type": "text", - "value": " ", + "value": " ", }, ], "depth": 0, @@ -443,21 +446,21 @@ Object { Object { "data": Array [ Object { - "number": "42", + "number ": " 42", }, ], "type": "text", - "value": "", + "value": "number :: 42", }, ], "depth": 2, "type": "block", - "value": "A heading outside", + "value": "## A heading outside", }, ], "depth": 1, "type": "block", - "value": "Alice", + "value": "# Alice", }, Object { "children": Array [ @@ -480,17 +483,17 @@ Object { ], "depth": 2, "type": "block", - "value": "Interests", + "value": "## Interests", }, ], "data": Array [ Object { - "age": "42", + "age ": " 42", }, ], "depth": 1, "type": "block", - "value": "Bob", + "value": "# Bob (age :: 42)", }, Object { "children": Array [ @@ -513,28 +516,28 @@ Object { ], "depth": 2, "type": "block", - "value": "Todo list", + "value": "## Todo list", }, Object { "children": Array [ Object { "data": Array [ Object { - "age": "56", + "age ": " 56", }, ], "type": "text", - "value": "Charlie is a good guy.", + "value": "Charlie is a good guy. (age :: 56)", }, ], "depth": 2, "type": "block", - "value": "Description", + "value": "## Description", }, ], "depth": 1, "type": "block", - "value": "Charlie", + "value": "# Charlie", }, ], "depth": 0, @@ -581,7 +584,7 @@ Hello world ], "depth": 1, "type": "block", - "value": "Some code", + "value": "# Some code ", }, ], "depth": 0, @@ -589,7 +592,7 @@ Hello world } `; -exports[`astDag normalize:false BlockIds 1`] = ` +exports[`astDag includePosition:true BlockIds 1`] = ` Object { "children": Array [ Object { @@ -618,6 +621,18 @@ And some more [[Bob Details#^2]]", "ids": Array [ "^1", ], + "position": Object { + "end": Object { + "column": 14, + "line": 4, + "offset": 35, + }, + "start": Object { + "column": 1, + "line": 4, + "offset": 22, + }, + }, "type": "block", "value": "## Section ^1", }, @@ -645,6 +660,18 @@ And some more [[Bob Details#^3 | Pointer to section 3]]", "ids": Array [ "^2", ], + "position": Object { + "end": Object { + "column": 14, + "line": 9, + "offset": 108, + }, + "start": Object { + "column": 1, + "line": 9, + "offset": 95, + }, + }, "type": "block", "value": "## Section ^2", }, @@ -672,6 +699,18 @@ And some more [[Unknown#^1]]", "ids": Array [ "^3", ], + "position": Object { + "end": Object { + "column": 14, + "line": 14, + "offset": 204, + }, + "start": Object { + "column": 1, + "line": 14, + "offset": 191, + }, + }, "type": "block", "value": "## Section ^3", }, @@ -688,6 +727,18 @@ And some more [[Unknown#^1]]", }, ], "depth": 2, + "position": Object { + "end": Object { + "column": 20, + "line": 19, + "offset": 279, + }, + "start": Object { + "column": 1, + "line": 19, + "offset": 260, + }, + }, "tags": Array [ "#Section", ], @@ -696,6 +747,18 @@ And some more [[Unknown#^1]]", }, ], "depth": 1, + "position": Object { + "end": Object { + "column": 20, + "line": 2, + "offset": 20, + }, + "start": Object { + "column": 1, + "line": 2, + "offset": 1, + }, + }, "type": "block", "value": "# Details about Bob", }, @@ -705,7 +768,7 @@ And some more [[Unknown#^1]]", } `; -exports[`astDag normalize:false Inline fields 1`] = ` +exports[`astDag includePosition:true Inline fields 1`] = ` Object { "children": Array [ Object { @@ -834,7 +897,7 @@ Object { } `; -exports[`astDag normalize:false Links 1`] = ` +exports[`astDag includePosition:true Links 1`] = ` Object { "children": Array [ Object { @@ -969,7 +1032,7 @@ Object { } `; -exports[`astDag normalize:false Outline 1`] = ` +exports[`astDag includePosition:true Outline 1`] = ` Object { "children": Array [ Object { @@ -1005,10 +1068,34 @@ Object { }, ], "ordered": false, + "position": Object { + "end": Object { + "column": 11, + "line": 7, + "offset": 62, + }, + "start": Object { + "column": 3, + "line": 7, + "offset": 54, + }, + }, "type": "outline", }, ], "ordered": false, + "position": Object { + "end": Object { + "column": 11, + "line": 7, + "offset": 62, + }, + "start": Object { + "column": 2, + "line": 5, + "offset": 33, + }, + }, "type": "outline", }, Object { @@ -1017,6 +1104,18 @@ Object { }, ], "ordered": false, + "position": Object { + "end": Object { + "column": 3, + "line": 9, + "offset": 74, + }, + "start": Object { + "column": 1, + "line": 2, + "offset": 1, + }, + }, "type": "outline", }, ], @@ -1025,7 +1124,7 @@ Object { } `; -exports[`astDag normalize:false People 1`] = ` +exports[`astDag includePosition:true People 1`] = ` Object { "children": Array [ Object { @@ -1043,11 +1142,35 @@ Object { }, ], "depth": 2, + "position": Object { + "end": Object { + "column": 21, + "line": 3, + "offset": 29, + }, + "start": Object { + "column": 1, + "line": 3, + "offset": 9, + }, + }, "type": "block", "value": "## A heading outside", }, ], "depth": 1, + "position": Object { + "end": Object { + "column": 8, + "line": 1, + "offset": 7, + }, + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, "type": "block", "value": "# Alice", }, @@ -1067,10 +1190,34 @@ Object { }, ], "ordered": false, + "position": Object { + "end": Object { + "column": 10, + "line": 12, + "offset": 96, + }, + "start": Object { + "column": 1, + "line": 11, + "offset": 78, + }, + }, "type": "outline", }, ], "depth": 2, + "position": Object { + "end": Object { + "column": 13, + "line": 9, + "offset": 76, + }, + "start": Object { + "column": 1, + "line": 9, + "offset": 64, + }, + }, "type": "block", "value": "## Interests", }, @@ -1081,6 +1228,18 @@ Object { }, ], "depth": 1, + "position": Object { + "end": Object { + "column": 18, + "line": 7, + "offset": 62, + }, + "start": Object { + "column": 1, + "line": 7, + "offset": 45, + }, + }, "type": "block", "value": "# Bob (age :: 42)", }, @@ -1100,10 +1259,34 @@ Object { }, ], "ordered": true, + "position": Object { + "end": Object { + "column": 16, + "line": 19, + "offset": 151, + }, + "start": Object { + "column": 1, + "line": 18, + "offset": 123, + }, + }, "type": "outline", }, ], "depth": 2, + "position": Object { + "end": Object { + "column": 13, + "line": 16, + "offset": 121, + }, + "start": Object { + "column": 1, + "line": 16, + "offset": 109, + }, + }, "type": "block", "value": "## Todo list", }, @@ -1120,11 +1303,35 @@ Object { }, ], "depth": 2, + "position": Object { + "end": Object { + "column": 15, + "line": 21, + "offset": 167, + }, + "start": Object { + "column": 1, + "line": 21, + "offset": 153, + }, + }, "type": "block", "value": "## Description", }, ], "depth": 1, + "position": Object { + "end": Object { + "column": 10, + "line": 14, + "offset": 107, + }, + "start": Object { + "column": 1, + "line": 14, + "offset": 98, + }, + }, "type": "block", "value": "# Charlie", }, @@ -1134,7 +1341,7 @@ Object { } `; -exports[`astDag normalize:false Yaml 1`] = ` +exports[`astDag includePosition:true Yaml 1`] = ` Object { "data": Array [ Object { @@ -1158,7 +1365,7 @@ Object { } `; -exports[`astDag normalize:false code fence 1`] = ` +exports[`astDag includePosition:true code fence 1`] = ` Object { "children": Array [ Object { @@ -1172,6 +1379,18 @@ Hello world }, ], "depth": 1, + "position": Object { + "end": Object { + "column": 13, + "line": 3, + "offset": 14, + }, + "start": Object { + "column": 1, + "line": 3, + "offset": 2, + }, + }, "type": "block", "value": "# Some code ", }, @@ -1181,88 +1400,677 @@ Hello world } `; -exports[`inLineAsArray inLineAsArray:false 1`] = ` +exports[`astDag normalize:true BlockIds 1`] = ` Object { "children": Array [ Object { - "data": Array [ + "children": Array [ Object { - "inline": "field", + "children": Array [ + Object { + "data": Array [ + Object { + "description": "Section 1", + }, + ], + "links": Array [ + Object { + "alias": undefined, + "type": "wikiLink", + "value": "Bob Details#^2", + }, + ], + "type": "text", + "value": "And some more [[Bob Details#^2]]", + }, + ], + "depth": 2, + "ids": Array [ + "1", + ], + "type": "block", + "value": "Section", }, - ], - "type": "text", - "value": "", - }, - Object { - "data": Array [ Object { - "inline": "field", + "children": Array [ + Object { + "data": Array [ + Object { + "description": "Section 2", + }, + ], + "links": Array [ + Object { + "alias": " Pointer to section 3", + "type": "wikiLink", + "value": "Bob Details#^3 ", + }, + ], + "type": "text", + "value": "And some more [[Bob Details#^3 | Pointer to section 3]]", + }, + ], + "depth": 2, + "ids": Array [ + "2", + ], + "type": "block", + "value": "Section", }, - ], - "type": "text", - "value": "nested", - }, - Object { - "data": Array [ Object { - "**Thoughts**": "It was decent.", + "children": Array [ + Object { + "data": Array [ + Object { + "description": "Section 3", + }, + ], + "links": Array [ + Object { + "alias": undefined, + "type": "wikiLink", + "value": "Unknown#^1", + }, + ], + "type": "text", + "value": "And some more [[Unknown#^1]]", + }, + ], + "depth": 2, + "ids": Array [ + "3", + ], + "type": "block", + "value": "Section", }, - ], - "type": "text", - "value": "", - }, - Object { - "data": Array [ Object { - "**Rating**": "6'", - }, - ], - "type": "text", + "children": Array [ + Object { + "data": Array [ + Object { + "description": "Section 4", + }, + ], + "type": "text", + "value": "", + }, + ], + "depth": 2, + "tags": Array [ + "Section", + ], + "type": "block", + "value": "Section", + }, + ], + "depth": 1, + "type": "block", + "value": "Details about Bob", + }, + ], + "depth": 0, + "type": "root", +} +`; + +exports[`astDag normalize:true Inline fields 1`] = ` +Object { + "children": Array [ + Object { + "data": Array [ + Object { + "inline": "field", + }, + ], + "type": "text", + "value": "", + }, + Object { + "data": Array [ + Object { + "inline": "field", + }, + ], + "type": "text", + "value": "nested", + }, + Object { + "data": Array [ + Object { + "**Thoughts**": "It was decent.", + }, + ], + "type": "text", + "value": "", + }, + Object { + "data": Array [ + Object { + "**Rating**": "6'", + }, + ], + "type": "text", + "value": "", + }, + Object { + "data": Array [ + Object { + "subject": Object { + "inline": "field", + }, + }, + ], + "type": "text", + "value": "", + }, + Object { + "data": Array [ + Object { + "subject": Object { + "inline": "field", + }, + }, + ], + "type": "text", + "value": "nested", + }, + Object { + "data": Array [ + Object { + "subject": Object { + "**Thoughts**": "It was decent.", + }, + }, + ], + "type": "text", + "value": "", + }, + Object { + "data": Array [ + Object { + "subject": Object { + "**Rating**": "6", + }, + }, + ], + "type": "text", + "value": "", + }, + Object { + "type": "text", + "value": "No inline fields", + }, + Object { + "data": Array [ + Object { + "Too": Object { + "many": Object { + "inline": "fields", + }, + }, + }, + ], + "type": "text", + "value": "", + }, + Object { + "data": Array [ + Object { + "[[Bob]]": Object { + "foaf:knows": "[[Alice]]", + }, + }, + ], + "links": Array [ + Object { + "alias": undefined, + "type": "wikiLink", + "value": "Alice", + }, + Object { + "alias": undefined, + "type": "wikiLink", + "value": "Bob", + }, + ], + "type": "text", + "value": "", + }, + ], + "depth": 0, + "type": "root", +} +`; + +exports[`astDag normalize:true Links 1`] = ` +Object { + "children": Array [ + Object { + "data": Array [ + Object { + "has image": "![Lovely image](../houses/img.png)", + }, + ], + "links": Array [ + Object { + "type": "link", + "value": "../houses/img.png", + }, + ], + "type": "text", "value": "", }, Object { + "links": Array [ + Object { + "alias": undefined, + "type": "wikiLink", + "value": "link 1", + }, + ], + "type": "text", + "value": "[[link 1]]", + }, + Object { + "links": Array [ + Object { + "alias": undefined, + "type": "wikiLink", + "value": "/full/path/link 1", + }, + ], + "type": "text", + "value": "[[/full/path/link 1]]", + }, + Object { + "links": Array [ + Object { + "alias": undefined, + "type": "wikiLink", + "value": "/full/path/link 1.md", + }, + ], + "type": "text", + "value": "[[/full/path/link 1.md]]", + }, + Object { + "links": Array [ + Object { + "alias": "Alias 2", + "type": "link", + "value": "http://example.com", + }, + ], + "type": "text", + "value": "[Alias 2](http://example.com)", + }, + Object { + "links": Array [ + Object { + "alias": "Alias 1", + "type": "wikiLink", + "value": "link 1 ", + }, + ], + "type": "text", + "value": "[[link 1 |Alias 1]]", + }, + Object { + "type": "text", + "value": "![[link 1]]", + }, + Object { + "type": "text", + "value": "![[image.png]]", + }, + Object { + "links": Array [ + Object { + "alias": undefined, + "type": "link", + "value": "http://example.com", + }, + ], + "type": "text", + "value": "The website is http://example.com see you!", + }, + Object { + "links": Array [ + Object { + "alias": "here", + "type": "link", + "value": "http://example.com", + }, + ], + "type": "text", + "value": "The website is [here](http://example.com) see you!", + }, + Object { + "links": Array [ + Object { + "type": "link", + "value": "protocol", + }, + ], + "type": "text", + "value": "The website is [here](protocol) see you!", + }, + Object { + "links": Array [ + Object { + "alias": undefined, + "type": "link", + "value": "https://example2.com", + }, + Object { + "alias": undefined, + "type": "link", + "value": "http://example.com", + }, + ], + "type": "text", + "value": " ", + }, + ], + "depth": 0, + "type": "root", +} +`; + +exports[`astDag normalize:true Outline 1`] = ` +Object { + "children": Array [ + Object { + "children": Array [ + Object { + "type": "outline", + "value": "Task", + }, + Object { + "checked": true, + "type": "outline", + "value": "Task", + }, + Object { + "type": "outline", + "value": "Item 0", + }, + Object { + "children": Array [ + Object { + "type": "outline", + "value": "Item 1", + }, + Object { + "type": "outline", + "value": "Item 2", + }, + Object { + "children": Array [ + Object { + "type": "outline", + "value": "Item 3", + }, + ], + "ordered": false, + "type": "outline", + }, + ], + "ordered": false, + "type": "outline", + }, + Object { + "type": "outline", + "value": "Item 4", + }, + ], + "ordered": false, + "type": "outline", + }, + ], + "depth": 0, + "type": "root", +} +`; + +exports[`astDag normalize:true People 1`] = ` +Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "data": Array [ + Object { + "number": "42", + }, + ], + "type": "text", + "value": "", + }, + ], + "depth": 2, + "type": "block", + "value": "A heading outside", + }, + ], + "depth": 1, + "type": "block", + "value": "Alice", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "type": "outline", + "value": "Eating", + }, + Object { + "type": "outline", + "value": "Dancing", + }, + ], + "ordered": false, + "type": "outline", + }, + ], + "depth": 2, + "type": "block", + "value": "Interests", + }, + ], "data": Array [ Object { - "subject": Object { - "inline": "field", + "age": "42", + }, + ], + "depth": 1, + "type": "block", + "value": "Bob", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "type": "outline", + "value": "Waking up", + }, + Object { + "type": "outline", + "value": "Drinking tea", + }, + ], + "ordered": true, + "type": "outline", + }, + ], + "depth": 2, + "type": "block", + "value": "Todo list", + }, + Object { + "children": Array [ + Object { + "data": Array [ + Object { + "age": "56", + }, + ], + "type": "text", + "value": "Charlie is a good guy.", + }, + ], + "depth": 2, + "type": "block", + "value": "Description", + }, + ], + "depth": 1, + "type": "block", + "value": "Charlie", + }, + ], + "depth": 0, + "type": "root", +} +`; + +exports[`astDag normalize:true Yaml 1`] = ` +Object { + "data": Array [ + Object { + "date updated": "2021-08-05T17:10:34+02:00", + "one": Array [ + "Two", + Object { + "Three": Array [ + "Other", + ], + }, + ], + }, + ], + "depth": 0, + "tags": Array [ + "cats", + "dogs", + ], + "type": "root", +} +`; + +exports[`astDag normalize:true code fence 1`] = ` +Object { + "children": Array [ + Object { + "children": Array [ + Object { + "lang": "js", + "type": "code", + "value": "\`\`\`js +Hello world +\`\`\`", + }, + ], + "depth": 1, + "type": "block", + "value": "Some code", + }, + ], + "depth": 0, + "type": "root", +} +`; + +exports[`inLineAsArray inLineAsArray:false 1`] = ` +Object { + "children": Array [ + Object { + "data": Array [ + Object { + "inline ": " field", + }, + ], + "type": "text", + "value": "inline :: field", + }, + Object { + "data": Array [ + Object { + " inline ": " field ", + }, + ], + "type": "text", + "value": "nested ( inline :: field )", + }, + Object { + "data": Array [ + Object { + "**Thoughts**": " It was decent.", + }, + ], + "type": "text", + "value": "**Thoughts**:: It was decent.", + }, + Object { + "data": Array [ + Object { + "**Rating**": " 6'", + }, + ], + "type": "text", + "value": "**Rating**:: 6'", + }, + Object { + "data": Array [ + Object { + "subject ": Object { + " inline ": " field", }, }, ], "type": "text", - "value": "", + "value": "subject :: inline :: field", }, Object { "data": Array [ Object { - "subject": Object { - "inline": "field", + " subject ": Object { + " inline ": " field ", }, }, ], "type": "text", - "value": "nested", + "value": "nested ( subject :: inline :: field )", }, Object { "data": Array [ Object { - "subject": Object { - "**Thoughts**": "It was decent.", + "subject ": Object { + " **Thoughts**": " It was decent.", }, }, ], "type": "text", - "value": "", + "value": "subject :: **Thoughts**:: It was decent.", }, Object { "data": Array [ Object { - "subject": Object { - "**Rating**": "6", + "subject ": Object { + " **Rating**": " 6", }, }, ], "type": "text", - "value": "", + "value": "subject :: **Rating**:: 6", }, Object { "type": "text", @@ -1271,21 +2079,21 @@ Object { Object { "data": Array [ Object { - "Too": Object { - "many": Object { - "inline": "fields", + "Too ": Object { + " many ": Object { + " inline ": " fields", }, }, }, ], "type": "text", - "value": "", + "value": "Too :: many :: inline :: fields", }, Object { "data": Array [ Object { - "[[Bob]]": Object { - "foaf:knows": "[[Alice]]", + "[[Bob]] ": Object { + " foaf:knows ": " [[Alice]]", }, }, ], @@ -1302,7 +2110,7 @@ Object { }, ], "type": "text", - "value": "", + "value": "[[Bob]] :: foaf:knows :: [[Alice]]", }, ], "depth": 0, @@ -1316,86 +2124,86 @@ Object { Object { "data": Array [ Array [ - "inline", - "field", + "inline ", + " field", ], ], "type": "text", - "value": "", + "value": "inline :: field", }, Object { "data": Array [ Array [ - "inline", - "field", + " inline ", + " field ", ], ], "type": "text", - "value": "nested", + "value": "nested ( inline :: field )", }, Object { "data": Array [ Array [ "**Thoughts**", - "It was decent.", + " It was decent.", ], ], "type": "text", - "value": "", + "value": "**Thoughts**:: It was decent.", }, Object { "data": Array [ Array [ "**Rating**", - "6'", + " 6'", ], ], "type": "text", - "value": "", + "value": "**Rating**:: 6'", }, Object { "data": Array [ Array [ - "subject", - "inline", - "field", + "subject ", + " inline ", + " field", ], ], "type": "text", - "value": "", + "value": "subject :: inline :: field", }, Object { "data": Array [ Array [ - "subject", - "inline", - "field", + " subject ", + " inline ", + " field ", ], ], "type": "text", - "value": "nested", + "value": "nested ( subject :: inline :: field )", }, Object { "data": Array [ Array [ - "subject", - "**Thoughts**", - "It was decent.", + "subject ", + " **Thoughts**", + " It was decent.", ], ], "type": "text", - "value": "", + "value": "subject :: **Thoughts**:: It was decent.", }, Object { "data": Array [ Array [ - "subject", - "**Rating**", - "6", + "subject ", + " **Rating**", + " 6", ], ], "type": "text", - "value": "", + "value": "subject :: **Rating**:: 6", }, Object { "type": "text", @@ -1404,21 +2212,21 @@ Object { Object { "data": Array [ Array [ - "Too", - "many", - "inline", - "fields", + "Too ", + " many ", + " inline ", + " fields", ], ], "type": "text", - "value": "", + "value": "Too :: many :: inline :: fields", }, Object { "data": Array [ Array [ - "[[Bob]]", - "foaf:knows", - "[[Alice]]", + "[[Bob]] ", + " foaf:knows ", + " [[Alice]]", ], ], "links": Array [ @@ -1434,7 +2242,7 @@ Object { }, ], "type": "text", - "value": "", + "value": "[[Bob]] :: foaf:knows :: [[Alice]]", }, ], "depth": 0, diff --git a/test/simpleAST.js b/test/simpleAST.js index f37cd45..0631c09 100755 --- a/test/simpleAST.js +++ b/test/simpleAST.js @@ -1,7 +1,6 @@ import { expect } from 'expect' -import { simpleAst } from '../src/simpleAst.js' +import { simpleAst } from '../index.js' import toMatchSnapshot from 'expect-mocha-snapshot' -import { createMarkdownParser } from '../src/markdown/markdownParser.js' import tests from './tests.js' import inlineFields from './support/inlineFields.js' @@ -11,43 +10,47 @@ describe('astDag', async function () { for (const current of tests) { it(current.title, async function () { const fullText = current.markdown - const parser = createMarkdownParser() - const astNode = parser.parse(fullText) - const result = simpleAst({ astNode, fullText }) + const result = simpleAst(fullText) expect(result).toMatchSnapshot(this) }) } }) -describe('astDag normalize:false', async function () { +describe('astDag normalize:true', async function () { for (const current of tests) { it(current.title, async function () { const fullText = current.markdown - const parser = createMarkdownParser() - const astNode = parser.parse(fullText) - const result = simpleAst({ astNode, fullText }, { normalize: false }) + const result = simpleAst(fullText, { normalize: true }) expect(result).toMatchSnapshot(this) }) } }) +describe('astDag includePosition:true', async function () { + for (const current of tests) { + it(current.title, async function () { + const fullText = current.markdown + const result = simpleAst(fullText, { includePosition: true }) + expect(result).toMatchSnapshot(this) + }) + } +}) + describe('inLineAsArray', async function () { it('inLineAsArray:true', async function () { const fullText = inlineFields.markdown - const parser = createMarkdownParser() - const astNode = parser.parse(fullText) - const result = simpleAst({ astNode, fullText }, { inlineAsArray: true }) + const result = simpleAst(fullText, { inlineAsArray: true }) + expect(result).toMatchSnapshot(this) }) it('inLineAsArray:false', async function () { const fullText = inlineFields.markdown - const parser = createMarkdownParser() - const astNode = parser.parse(fullText) - const result = simpleAst({ astNode, fullText }, { inlineAsArray: false }) + const result = simpleAst(fullText, { inlineAsArray: false }) + expect(result).toMatchSnapshot(this) }) })