markdown-js icon indicating copy to clipboard operation
markdown-js copied to clipboard

toMarkdown() method

Open christianvoigt opened this issue 10 years ago • 7 comments
trafficstars

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?

christianvoigt avatar May 11 '15 14:05 christianvoigt

Yep, a markdown.toMarkdown('Hello <b>world</b>!') would be awesome. Like, two way parsing/encoding/etc.

pedrosanta avatar May 21 '15 08:05 pedrosanta

http://domchristie.github.io/to-markdown/

kulikalov avatar Aug 12 '15 23:08 kulikalov

+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.

ffffranklin avatar Oct 08 '15 23:10 ffffranklin

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!

evilstreak avatar Oct 09 '15 08:10 evilstreak

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 avatar Sep 03 '16 20:09 sebastienbarre

@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 avatar Sep 03 '16 20:09 strugee

@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

sebastienbarre avatar Sep 03 '16 21:09 sebastienbarre