Skip to content

Markdown Contextualizing

What is the best Markdown parser?

Requirements

Steps


import fs from 'node:fs/promises'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {frontmatter} from 'micromark-extension-frontmatter'
import {frontmatterFromMarkdown, frontmatterToMarkdown} from 'mdast-util-frontmatter'
import util from "util";


const doc = await fs.readFile('./dentropydaemon-wiki/Wiki/Software/List/docker.md')
console.log(doc.toString())
const tree = fromMarkdown(doc, {
  extensions: [frontmatter(['yaml', 'toml'])],
  mdastExtensions: [frontmatterFromMarkdown(['yaml', 'toml'])]
})

// console.log(tree)
console.log(util.inspect(tree, {showHidden: false, depth: null, colors: true}))

SELECT * FROM pkm;

SELECT DISTINCT json_extract(markdown_syntax_tree , '$.type') AS key
FROM pkm;

SELECT DISTINCT json_extract(markdown_syntax_tree , '$.children') AS key
FROM pkm;


SELECT DISTINCT json_extract(markdown_syntax_tree , '$.children') AS key
FROM (SELECT * FROM pkm LIMIT 1);

SELECT DISTINCT json_extract(markdown_syntax_tree , '$.children') AS key
FROM (SELECT * FROM pkm LIMIT 1 OFFSET 1);


SELECT DISTINCT json_extract(markdown_syntax_tree , '$.children') AS key
FROM (SELECT * FROM pkm LIMIT 1 OFFSET 1);

SELECT DISTINCT json_extract(markdown_syntax_tree , '$.children[0].children') AS key
FROM (SELECT * FROM pkm LIMIT 1 OFFSET 1);

-- We need to take the firsdb.close();t child element
-- Loop through it's list of child element 
-- If I can loop through all these and put them in a table that would be great


-- Here we have the list expanded out
SELECT * FROM
json_each(
    (json_extract((SELECT markdown_syntax_tree FROM pkm LIMIT 1 OFFSET 1) , '$.children[0].children'))
);


SELECT * FROM
json_each(
    (json_extract((SELECT markdown_syntax_tree FROM pkm LIMIT 1 OFFSET 1) , '$'))
);

-- So I need to check the key then make a list then check the key
-- The inside query checks the key, that is surrounded by the query that makes the list, then recursive
-- We already check the key then check the list

SELECT * FROM
json_each(
    (json_extract((SELECT markdown_syntax_tree FROM pkm LIMIT 1 OFFSET 1) , '$.children'))
);

SELECT * FROM
json_each(
    (json_extract((SELECT markdown_syntax_tree FROM pkm LIMIT 1 OFFSET 1) , '$.children[0].children'))
);

SELECT * FROM
json_each(
    (json_extract((SELECT markdown_syntax_tree FROM pkm LIMIT 1 OFFSET 1) , '$.children[0].children[0].children'))
);

SELECT * FROM
json_each(
    (json_extract((SELECT markdown_syntax_tree FROM pkm LIMIT 1 OFFSET 1) , '$.children[0].children[0].children[0].children'))
);

-- Now how do we put this together
-- Well remember we are transorming tables
-- What is our output table
-- We need to check if the children element is present, if it is we do something
-- But we do not have if and else
-- But we do have where
-- But how do we start off the query
-- That's a good point
-- Hmmm
-- We create a huge table then save all the objects that do not have the children key
-- So we are going to save the key's to all these objects recursively
-- Wait don't we have json_tree as a thing
-- json_tree needs to be loaded


SELECT * FROM
json_tree  ((SELECT markdown_syntax_tree FROM pkm LIMIT 1 OFFSET 1));


--------------------------

SELECT * FROM
pkm, json_tree(pkm.markdown_syntax_tree);

SELECT * FROM
pkm, json_tree(pkm.markdown_syntax_tree)
WHERE key = 'url';

SELECT file_name, value FROM
pkm, json_tree(pkm.markdown_syntax_tree)
WHERE key = 'url';

SELECT file_name, value FROM
pkm, json_tree(pkm.markdown_syntax_tree)
WHERE key = 'permalink';