• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

sparkartgroup/gulp-markdown-to-json: Parse Markdown and YAML → compile Markdown ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

sparkartgroup/gulp-markdown-to-json

开源软件地址(OpenSource Url):

https://github.com/sparkartgroup/gulp-markdown-to-json

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

gulp-markdown-to-json

CircleCI Status Semistandard Style

Parse Markdown + YAML, compile Markdown to HTML, wrap it in JSON.

The neutral format of JSON opens new possibilities for your handcrafted content. Send it onward to other plugins such as gulp-hb. When their powers combine, you get a static site generator! Pipe to request to send it to a search index or import into a CMS. Write a plugin to tap into the stream if you need a client library.

Table of Contents

Install

npm install gulp-markdown-to-json --save-dev

Dependencies

This plugin does not bundle any Markdown parser to keep your options open. BYOM!

Pick one of these or write a new parser for fun!

Install, configure, and pass a rendering method to this plugin with a source string as its first argument. Output goes straight into the JSON file’s body property. If your parser requires instantiation pass a context to call it with by defining context in the config object. If yet more hoop jumping is required, write a wrapper function such as this example for remark:

function render (string) {
  const remark = require('remark');
  const html = require('remark-html');
  return remark().use(html).process(string).toString();
}

Note: YAML frontmatter blocks are stripped and handled before Markdown rendering with front-matter

Usage

/gulpfile.js

const gulp = require('gulp');
const markdownToJSON = require('gulp-markdown-to-json');
const marked = require('marked');

marked.setOptions({
  pedantic: true,
  smartypants: true
});

gulp.task('markdown', () => {
  gulp.src('./content/**/*.md')
    .pipe(markdownToJSON(marked))
    .pipe(gulp.dest('.'))
});

Transformed source files flow onward to the destination of your choice with directory structure preserved. Any valid JSON files matched by your gulp.src glob passthrough.

/blog/posts/bushwick-artisan.md

---
slug: bushwick-artisan
title: Wes Anderson pop-up Bushwick artisan
layout: centered
---

## YOLO
Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.

/blog/posts/bushwick-artisan.json

{
  "slug": "bushwick-artisan",
  "title": "Wes Anderson pop-up Bushwick artisan",
  "layout": "centered",
  "body": "<h2 id=\"yolo\">YOLO</h2>\n<p>Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.</p>",
  "updatedAt": "1970-01-01T00:00:00Z"
}

Consolidated Output

Gather Markdown files before piping with list-stream to combine output into a single JSON file. Directory structure is preserved and represented as nested JSON for iteration with Handlebars.js and friends. This is handy for navigation and other global content. Valid JSON files are included in the object if matched by your gulp.src glob.

The consolidated file is named content.json by default and optionally renamed.

const gulp = require('gulp');
const ListStream = require('list-stream');
const markdownToJSON = require('gulp-markdown-to-json');
const marked = require('marked');

gulp.task('markdown', () => {
  gulp.src('./content/**/*.md')
    .pipe(ListStream.obj())
    .pipe(markdownToJSON(marked, 'blog.json'))
    .pipe(gulp.dest('.'))
});

blog.json

{
  "blog": {
    "blog": {
      "title": "ipsum dipsum",
      "body": "<p>From west to \"east\"!</p>",
      "updatedAt": "1970-01-01T00:00:00Z"
    },
    "posts": {
      "bushwick-artisan": {
        "slug": "bushwick-artisan",
        "title": "Wes Anderson pop-up Bushwick artisan", 
        "layout": "centered",
        "body": "<h2 id=\"yolo\">YOLO</h2>\n<p>Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.</p>",
        "updatedAt": "1970-01-01T00:00:00Z"
      }
    }
  },
  "mission": {
    ...
  }
}

Specify flattenIndex: true in the config object to unwrap home page/index-style content and merge it into the parent object. Name these files index or the same as a parent directory.

{
  "blog": {
    "title": "ipsum dipsum",
    "body": "<p>From west to \"east\"!</p>",
    "updatedAt": "1970-01-01T00:00:00Z",
    "posts": {
      ...
    }
  },
  "mission": {
    ...
  }
}

This avoids redundant-feeling blog.blog scenarios when iterating and selecting from this content.

Title Extraction and Stripping

Define titles as title in the YAML frontmatter. Text of the first <h1> is assigned to title automatically if this is not specified.

Specify stripTitle: true in the config object to remove the first <h1> from the body. Use this if you are displaying the title outside of the body, in a page header for example.

/blog/posts/bushwick-artisan.md

Wes Anderson pop-up Bushwick artisan
====================================

## YOLO
Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.

/blog/posts/bushwick-artisan.json

{
  "title": "Wes Anderson pop-up Bushwick artisan", 
  "body": "<h2 id=\"yolo\">YOLO</h2>\n<p>Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.</p>",
  "updatedAt": "1970-01-01T00:00:00Z"
}

Transforms

To change or add to the JSON data for each file, specify a transform function and return your desired object. This function is passed the default data and the Vinyl file object for the source file.

For example:

gulp.src('./content/**/*.md')
  .pipe(ListStream.obj())
  .pipe(markdownToJSON(marked, 'blog.json', (data, file) => {
    delete data.body;
    data.path = file.path;
    return data;
  }))
  .pipe(gulp.dest('.'))

API

markdownToJSON((renderer: Function, name?: String, transform?: Function) | config: Object) => TransformStream

config

  • renderer Function accepts Markdown source string, returns an escaped HTML string. Required
  • context Object to use when calling renderer
  • name String to rename consolidated output file, if using. Default: content.json
  • flattenIndex Boolean unwrap files named index or after parent dirs in consolidated output. Default: false
  • stripTitle Boolean strips the first <h1> from body, if extracted as title. Default: false
  • transform Function to access and change the JSON data for each file before outputting

Contribute

Pull requests accepted!

License

MIT
Copyright © 2017 Sparkart Group, Inc.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap