UNPKG

markdown-to-html

Version:

Converts markdown text to HTML. A readable stream plus utilities and web demo.

96 lines (88 loc) 11.5 kB
<!DOCTYPE html> <html> <head> <title>README.md</title> <link rel="stylesheet" href="test/style.css"> </head> <body> <h1 id="markdown-to-html">markdown-to-html</h1> <p>Command-line utility to convert Github Flavored Markdown to HTML.<br>Output may be to stdout or to your default browser.<br>Also, the underlying Markdown and GithubMarkdown classes are readable stream classes<br>and may be used however you like (e.g., pipe to an http response or to stdout).<br>Includes a demo of a web server app that uses both the classes.</p> <h2 id="installation">Installation</h2> <h4 id="to-use-the-command-line-utilities">To use the command line utilities</h4> <pre><code><div class="highlight"><pre><span class="nx">npm</span> <span class="nx">install</span> <span class="nx">markdown</span><span class="o">-</span><span class="nx">to</span><span class="o">-</span><span class="nx">html</span> <span class="o">-</span><span class="nx">g</span> </pre></div> </code></pre><h4 id="to-use-the-markdown-or-githubmarkdown-classes-in-your-project">To use the Markdown or GithubMarkdown classes in your project</h4> <pre><code><div class="highlight"><pre><span class="nx">npm</span> <span class="nx">install</span> <span class="nx">markdown</span><span class="o">-</span><span class="nx">to</span><span class="o">-</span><span class="nx">html</span> <span class="o">--</span><span class="nx">save</span> </pre></div> </code></pre><h2 id="example-usage">Example Usage</h2> <h4 id="command-line-utility-to-output-html-to-stdout">Command line utility to output HTML to stdout</h4> <pre><code><div class="highlight"><pre><span class="nx">markdown</span> <span class="nx">myfile</span><span class="p">.</span><span class="nx">md</span> <span class="p">[</span><span class="o">&lt;</span><span class="nx">options</span><span class="o">&gt;</span><span class="p">]</span> </pre></div> </code></pre><h4 id="command-line-utility-to-output-html-to-default-browser">Command line utility to output HTML to default browser</h4> <pre><code><div class="highlight"><pre><span class="nx">markdownb</span> <span class="nx">myfile</span><span class="p">.</span><span class="nx">md</span> <span class="p">[</span><span class="o">&lt;</span><span class="nx">options</span><span class="o">&gt;</span><span class="p">]</span> </pre></div> </code></pre><h4 id="command-line-utility-to-output-the-github-api-results-to-stdout">Command line utility to output the Github API results to stdout</h4> <pre><code><div class="highlight"><pre><span class="nx">github</span><span class="o">-</span><span class="nx">markdown</span> <span class="nx">myfile</span><span class="p">.</span><span class="nx">md</span> <span class="p">[</span><span class="o">&lt;</span><span class="nx">options</span><span class="o">&gt;</span><span class="p">]</span> </pre></div> </code></pre><h4 id="command-line-utility-to-output-the-github-api-results-to-default-browser">Command line utility to output the Github API results to default browser</h4> <pre><code><div class="highlight"><pre><span class="nx">github</span><span class="o">-</span><span class="nx">markdownb</span> <span class="nx">myfile</span><span class="p">.</span><span class="nx">md</span> <span class="p">[</span><span class="o">&lt;</span><span class="nx">options</span><span class="o">&gt;</span><span class="p">]</span> </pre></div> </code></pre><h4 id="run-the-web-demo">Run the web demo</h4> <ol> <li>Run <code>git clone https://github.com/cwjohan/markdown-to-html.git</code> to create a markdown-to-html directory.</li> <li>Run <code>cd markdown-to-html</code></li> <li>Run <code>npm install</code></li> <li>Run <code>npm start</code>.</li> <li>In a web browser address field type <a href="http://localhost:3000">localhost:3000</a>.</li> </ol> <h3 id="use-the-markdown-class-to-render-markdown-text">Use the Markdown class to render markdown text</h3> <pre><code class="lang-js"><div class="highlight"><pre><span class="kd">var</span> <span class="nx">Markdown</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;markdown-to-html&#39;</span><span class="p">).</span><span class="nx">Markdown</span><span class="p">;</span> <span class="kd">var</span> <span class="nx">md</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Markdown</span><span class="p">();</span> <span class="nx">md</span><span class="p">.</span><span class="nx">bufmax</span> <span class="o">=</span> <span class="mi">2048</span><span class="p">;</span> <span class="kd">var</span> <span class="nx">fileName</span> <span class="o">=</span> <span class="s1">&#39;test/test.md&#39;</span><span class="p">;</span> <span class="kd">var</span> <span class="nx">opts</span> <span class="o">=</span> <span class="p">{</span><span class="nx">title</span><span class="o">:</span> <span class="s1">&#39;File $BASENAME in $DIRNAME&#39;</span><span class="p">,</span> <span class="nx">stylesheet</span><span class="o">:</span> <span class="s1">&#39;test/style.css&#39;</span><span class="p">};</span> <span class="p">...</span> <span class="c1">// Write a header.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;===============================&#39;</span><span class="p">);</span> <span class="c1">// Write a trailer at eof.</span> <span class="nx">md</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;===============================&#39;</span><span class="p">);</span> <span class="p">});</span> <span class="nx">md</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span><span class="nx">fileName</span><span class="p">,</span> <span class="nx">opts</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="s1">&#39;&gt;&gt;&gt;&#39;</span> <span class="o">+</span> <span class="nx">err</span><span class="p">);</span> <span class="nx">process</span><span class="p">.</span><span class="nx">exit</span><span class="p">();</span> <span class="p">}</span> <span class="nx">md</span><span class="p">.</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">stdout</span><span class="p">);</span> <span class="p">});</span> </pre></div> </code></pre> <h2 id="options-for-markdown-and-markdownb">Options for markdown and markdownb</h2> <h4 id="-flavor-type-"><code>--flavor &lt;type&gt;</code></h4> <p>Format as type &#39;gfm&#39; or just plain &#39;markdown&#39;. May be abbreviated <code>-f</code> on the command line.<br>Note that for the <code>github-markdown</code> utility or the <code>GithubMarkdown</code> class it is the &#39;markdown&#39; flavor that gives you<br>something resembling the README.md format on Github. Whereas, the &#39;gfm&#39; flavor gives you something resembling the<br>the format of comments and issues on Github. This is due to how the Github Markdown API works. On the other hand,<br>for the <code>markdown</code> utility or the <code>Markdown</code> class it is virtually the opposite. For example, fenced code blocks<br>don&#39;t work when using the <code>markdown</code> utility or the <code>Markdown</code> class with the <code>--flavor markdown</code> option. This is<br>due to how the <code>marked</code> markdown parser works. The default value for this option is whichever flavor results in<br>something more like README.md format.</p> <h4 id="-highlight-"><code>--highlight</code></h4> <p>Highlight code blocks with style info. Highlight has no effect in github-markdown.<br>May be abbreviated <code>-h</code> on the command line. Defaults to <code>false</code>.</p> <h4 id="-stylesheet-stylesheet-"><code>--stylesheet &lt;stylesheet&gt;</code></h4> <p>Outputs HTML header with link element referring to the given stylesheet.<br>May be abbreviated <code>-s</code> on the command line.</p> <h4 id="-title-title-"><code>--title &lt;title&gt;</code></h4> <p>Outputs HTML header with given title. Title string may include special values<br><code>$FILENAME</code>, <code>$DIRNAME</code>, <code>$BASENAME</code>, or <code>$PATHNAME</code> variables which are replaced by the<br>corresponding .md filename, directory name, base name, or full path, respectively.<br>Alternatively, the title may be any text you wish. May be abbreviated <code>-t</code> on the command line.</p> <h4 id="-context-context-"><code>--context &lt;context&gt;</code></h4> <p>Suupply the relevant Github user/project to use with #&lt;n&gt; issue number references. Typically, these are<br>not used in README.md files but, rather, in comments and issue text on Github.<br>May be abbreviated <code>-c</code> on the command line.</p> <h4 id="-verbose-"><code>--verbose</code></h4> <p>Verbose output. May be abbreviated <code>-v</code> on the command line. Defaults to <code>false</code> unless debug has<br>been specified, in which case it is set to <code>true</code>. Only used by the command line utilities.</p> <h4 id="-debug-"><code>--debug</code></h4> <p>Debug output to stderr. For example, outputs the individual chunks of data pushed to output.<br>May be abbreviated <code>-d</code> on the command line. Defaults to <code>false</code>. Used only by the command line<br>utilities. However, the Markdown and GithubMarkdown classes both have a <code>debug</code> property.</p> <h4 id="-help-"><code>--help</code></h4> <p>Output usage info. Only used by the command line utilities.</p> <h2 id="markdown-and-githubmarkdown-class-properties">Markdown and GithubMarkdown class properties</h2> <h4 id="-bufmax-"><code>bufmax</code></h4> <p>The chunk size for streaming -- that is, the maximum amout of data to push to the read<br>operation at any given time. Defaults to 1024.</p> <h4 id="-debug-"><code>debug</code></h4> <p>Debug output to stderr. For example, outputs the individual chunks of data pushed to output.<br>Defaults to <code>false</code>.</p> <h2 id="markdown-and-githubmarkdown-class-methods">Markdown and GithubMarkdown class methods</h2> <h4 id="-render-filename-opts-ondone-"><code>render(fileName, opts, onDone)</code></h4> <p>Renders the markdown text in the given file using the given<br>options. Calls the onDone callback function when rendering is finished, if specified. If you are going to<br>pipe the output to another stream, this is best done in the callback function.</p> <p>The <code>onDone</code> callback takes a single error parameter, which ought to be tested before<br>performing any other operations on the Markdown or GithubMarkdown stream.</p> <p>Before calling <code>render</code>, you can set up a test for end of file with <code>on(&#39;end&#39;, cb)</code>, which is a good place<br>to write any output that should follow the streamed HTML. See the above code example for how to do that.</p> </body> <html>