Skip to content

Instantly share code, notes, and snippets.

@hail2u
Last active April 15, 2016 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hail2u/02a773e5c1d268c8e6447f7c84fc743e to your computer and use it in GitHub Desktop.
Save hail2u/02a773e5c1d268c8e6447f7c84fc743e to your computer and use it in GitHub Desktop.
<section>...</section>とか書いてもその中もMarkdownとして処理するやつ
#!/usr/bin/env node
"use strict";
var marked = require("marked");
var md = `# Main Section
This is a paragraph.
<section>
# Sub Section
This is a paragraph in sub section.
</section>
<figure>
> This is a quoted paragraph in figure element.
<figcaption>— John Doe</figcaption>
</figure>
End of test.
`;
var renderer = new marked.Renderer();
function html(html) {
var attributes;
var contents;
var tag;
var tokens = html.trim().match(/^<(\w+)(.*?)>([\s\S]*)<\/\1>/);
var tags = ["aside", "figure", "section"];
tag = tokens[1];
if (tags.indexOf(tag) === -1) {
return html;
}
attributes = tokens[2];
contents = marked(tokens[3].replace(/&gt;/g, ">"), {
renderer: renderer
}).trim();
return "<" + tag + attributes + ">\n" + contents + "\n</" + tag + ">\n";
}
renderer.html = html;
console.log(marked(md, {
renderer: renderer
}));

デフォルトではsection要素などを使うとその中はMarkdownとしてはうまく処理されない(インラインの記法だけ処理される)。

<h1 id="main-section">Main Section</h1>
<p>This is a paragraph.</p>
<section>
# Sub Section

This is a paragraph in sub section.
</section>

<figure>
&gt; This is a quoted paragraph in figure element.

<figcaption>— John Doe</figcaption>
</figure>

<p>End of test.</p>

それが、こうなりだいたいうまく処理される。

<h1 id="main-section">Main Section</h1>
<p>This is a paragraph.</p>
<section>
<h1 id="sub-section">Sub Section</h1>
<p>This is a paragraph in sub section.</p>
</section>
<figure>
<blockquote>
<p>This is a quoted paragraph in figure element.</p>
</blockquote>
<figcaption>— John Doe</figcaption>
</figure>
<p>End of test.</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment