Open christianvoigt opened 9 years ago
Yep, a markdown.toMarkdown('Hello <b>world</b>!')
would be awesome. Like, two way parsing/encoding/etc.
+1
@pedrosanta I think @christianvoigt is alluding to the "markdown tree" that's returned from the public Markdown.parse()
method (which depends on private method Markdown.prototype.toTree()
). I agree with @christianvoigt, it would be nice to have a method that could render the intermediate "markdown tree" as markdown in any of the supported dialects (https://github.com/evilstreak/markdown-js/tree/master/src/dialects).
Example:
var markdown = require('markdown').markdown,
dialect = undefined, // Markdown defaults to Gruber dialect
source = '#heading\n\ncontent\n\n * list',
tree = markdown.parse(source, dialect),
doc = markdown.toMarkdown(tree, dialect);
console.log(tree);
// ['markdown',
// ['header', { level: 1 }, 'heading'],
// ['para', 'content'],
// ['bulletlist', ['listitem', 'list']]]
console.log(doc);
// # heading
//
// content
//
// * list
With such a method it would be possible to create markdown documents from custom data structures. A todo app would be a good example. You could render a collection of task object literals as a markdown document.
Converting a markdown tree into markdown isn't possible at the moment.
I'd be happy to include such a feature in this library if anyone wants to write one!
Just checking if you guys ever found a solution to this one. I'm also trying to find a way to parse, update, and re-create a Markdown file... Thanks.
@sebastienbarre I gave up and switched to remark (remark.js.org) which as a bonus is better documented - at least last time I checked.
@strugee thanks a lot, this is great.
If somebody else finds this issue, here is how it can be done with remark
. The below example insert a TOC in your Markdown.
const markdown = require('remark-parse');
const report = require('vfile-reporter');
const stringify = require('remark-stringify');
const toc = require('remark-toc');
const unified = require('unified');
const example = `
# Alpha
## Table of Contents
## Bravo
### Charlie
## Delta
`;
unified()
.use(markdown)
.use(toc)
.use(stringify)
.process(example, (err, file) => {
console.error(report(err || file));
console.log(file.toString());
});
which will output:
# Alpha
## Table of Contents
- [Bravo](#bravo)
- [Charlie](#charlie)
- [Delta](#delta)
## Bravo
### Charlie
## Delta
Hi, I would like to make some changes to the markdown tree and then generate the resulting Html as well as the updated Markdown. So far I haven't found a way to transform the JsonML tree back into Markdown. Is there any way to do this?