Skip to content

Commit

Permalink
feat: add the includePosition option
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianvasquez committed Jan 3, 2023
1 parent b504ef6 commit 3164e4f
Show file tree
Hide file tree
Showing 7 changed files with 1,049 additions and 184 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,7 +22,7 @@ Say you have the following markdown
---
hello: world
---

# Heading 1

Some text under Heading 1
Expand All @@ -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
Expand Down Expand Up @@ -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.
20 changes: 20 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -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))
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 }
13 changes: 13 additions & 0 deletions playground/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Playground

To run

```sh
npm run install
```

Then

```sh
npm run dev
```
34 changes: 22 additions & 12 deletions src/simpleAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 3164e4f

Please sign in to comment.