preface

With the popularity of wordpress and static websites, Markdown is being used more and more.

Markdown is a writing-oriented grammar engine. The ultimate goal of MarkDown is to parse into HTML for web browsing, so it is HTML compatible, meaning you can use native HTML tags in Markdown documents.

Markdown parser

When you develop static web generators, you use a format called front Matter to write your web content in a format like this

-- Title: To play markdown, you will need these tools. With the popularity of wordpress and static websites, Markdown is being used more and more. .Copy the code

When a website is generated, it needs markdown parsing and then rendering into HTML pages. What tools do you use for parsing?

marked

Marked is the first Markdown parser developed in Node.js that provides both CLI command calls and Node.js API calls.

Example of CLI invocation code

$ marked -o hello.html
hello world
^D
$ cat hello.html

hello world

Copy the code

Example API calls

var marked = require('marked');
console.log(marked('I am using __markdown__.'));

Copy the code

These are some common features, but marked also supports code highlighting by using highlight.js.

Code highlighting with highlight.js is marked in that it supports API calls and is marked in that it is highlighted

marked.setOptions({ highlight: function (code, lang) { var res; if (lang) { res = hljs.highlight(lang, code, true).value; } else { res = hljs.highlightAuto(code).value; } return res; }});Copy the code

The generated code already contains code highlighting tags, which are displayed by simply introducing the highlight.js theme, where all the color themes are

markdown-js

Markdown-js is also a markdown parser developed in Node.js. The basic usage of markdown is similar to that of marked, but the documentation does not mention an interface for code highlighting like marked.

Markdown generator

to-markdown

What is a Markdown generator? It generates markdown files based on HTML tags.

The markdown generator on Github has the highest star count of to-markdown.

Simple code example

var toMarkdown = require('to-markdown');
toMarkdown('

Hello world!

'); Copy the code

To-markdown was recently updated to add GFM compatibility, which means “GFM”, because github has extended the markdown syntax.

Examples using GFM

toMarkdown('Hello world!', { gfm: true });
Copy the code

So what’s the use of this to-markdown?

For a simple example, let’s say I want to develop a simple RSS reader, but I don’t want to jump to a target site to read it, because different sites have different styles, resulting in an inconsistent reading experience.

What to do? Grab the site content, then use to-Markdown to generate a Markdown file, and then use your own template styles for uniform rendering.

Of course, removing ads is just a side effect.

heckyesmarkdown

In addition to to-markdown, there is also a relatively useful API, HeckyesMarkdown. This project uses PHP-readability to improve the readability of articles.

Unfortunately heckyesMarkdown is not open source. The project is a bit old, and I guess Github didn’t catch on at that time.

Heckyesmarkdow’s support for Chinese is not very friendly, if you want to crawl Chinese stations or use to-Markdown is more reliable.

front matter

Markdown is really convenient to write articles, simple and easy to learn, but Markdown cannot save metadata, such as author, date, type and other structured data. If they all generate HTML tags, it is a little more difficult to extract them, and they have to use Cheerio to complete them.

So, in order to save metadata for articles easily, almost all static web site generators use front Matter format to save articles.

A front Matter file is usually divided into a header and a body part. The header format is yamL, TOML, and JSON. The front Matter parsing tool needs to identify the headers in these three formats. The body is just plain Markdown content.

front-matter

Front-matter is also developed in Node.js and the Fornt-matter parser is much simpler than the Markdown parser.

Example file example.md

---
title: Just hack'n
description: Nothing to see here
---

This is some text about some stuff that happened sometime ago
Copy the code

Parsing code

var fs = require('fs')
  , fm = require('front-matter')

fs.readFile('./example.md', 'utf8', function(err, data){
  if (err) throw err

  var content = fm(data)

  console.log(content)
})
Copy the code
{
    attributes: {
        title: 'Just hack\'n',
        description: 'Nothing to see here'
    },
    body: '\nThis is some text about some stuff that happened sometime ago',
    frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here'
}
Copy the code

Although the format may seem inconsistent, front Matter is a strong complement to MarkDown.

Please reserve the address of this article for reprint:
To play Markdown, you’ll need these tools