Calling twice mdast-util-from-markdown with micromark-extension-frontmatter returns frontmatter only for the first file
Closed this issue · 3 comments
crystalfp commented
Subject of the issue
Calling mdast-util-from-markdown
with mdast-util-frontmatter
and micromark-extension-frontmatter
twice in sequence returns YAML front matter only for the first file.
Your environment
- OS: Windows 10 64 bits
- Packages:
"mdast-util-from-markdown": "^0.6.0",
"mdast-util-frontmatter": "^0.2.0",
"micromark-extension-frontmatter": "^0.2.0" - Env: node 14.8.0; npm 6.14.7
Steps to reproduce
var fs = require('fs')
var fromMarkdown = require('mdast-util-from-markdown')
var syntax = require('micromark-extension-frontmatter')
var frontmatter = require('mdast-util-frontmatter')
const options0 = {
extensions: [syntax({type: "yaml", marker: {open: "-", close: "."}})],
mdastExtensions: [frontmatter.fromMarkdown(["yaml"])]
};
const options1 = {
extensions: [syntax({type: "yaml", marker: {open: "-", close: "."}})],
mdastExtensions: [frontmatter.fromMarkdown(["yaml"])]
};
const options2 = {
extensions: [syntax({type: "yaml", marker: {open: "-", close: "."}})],
mdastExtensions: [options0.mdasExtensions[0]]
};
const options3 = {
extensions: [options0.extensions[0]],
mdastExtensions: [frontmatter.fromMarkdown(["yaml"])]
};
const doc = fs.readFileSync("example.md", "utf-8");
const t1 = fromMarkdown(doc, options0);
console.log(JSON.stringify(t1, undefined, 2));
const t2 = fromMarkdown(doc, options0); // see below
console.log(JSON.stringify(t2, undefined, 2));
The input file example.md
contains:
---
title: 'CouchDB tool'
type: ToDo
status: Hot
...
# CouchDB to do
Split long text blocks
Does not matter if the two fromMarkdown()
are called with the same or with different inputs.
Expected behavior
Both parse should return a block of "type": "yaml"
.
Actual behavior
- Calling both
fromMarkdown()
withoptions0
fails.
The yaml block from the first file is parsed as"type": "yaml"
as it should. Instead the second time it is parsed as"type": "thematicBreak" + "type": "paragraph"
- Calling second
fromMarkdown()
withoptions1
succeeds (completely separated option objects) - Calling second
fromMarkdown()
withoptions2
succeeds (they share the call tomdast-util-frontmatter
) - Calling second
fromMarkdown()
withoptions3
fails (they share the call tomicromark-extension-frontmatter
) as case 1. This seems to point to something not reset insyntax()
, but equally could be a problem withmdast-util-from-markdown
for whichfromMarkdown()
does not re-initialize extensions at each call.
crystalfp commented
Workaround. Call both fromMarkdown()
with the same option object. But before the second call add:
options.extensions[0] = syntax({type: "yaml", marker: {open: "-", close: "."}});
I don't like it, options should not carry status in my opinion, but at least I can continue with my experiments.
wooorm commented
Thanks that helped, pretty deep down, but working on a fix!
crystalfp commented
Tested. It works without my workaround! Thanks!