diff --git a/README.md b/README.md index 35fc44d..346423a 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,25 @@ $ hexo migrate github-issue owner/repo [--publish] It will migrate owner/repo issue to hexo, there are some specific issue label is supported: - "category_", set the sepecfic category for the post -- "top_", set the top priority for the post. - "draft", the post layout will be set to "draft", that means the post will be stored in draft dir - "publish", if user migrate with "--publish" opt, only post with "publish" label will be created. +### Front-matter support +If you specify the front-matter in the issue content, like: + +``` +--- +cover: http://demo.jpeg +top: 1 +--- +# Title +Hello World! +``` + +The front-matter will be transparently added in your post writings. + +Note that, the front-matter in the issue content has the highest priority, that means the internal front-matter which generated by migrator (such as, `title`, `tags`, `number`, `date`) will also be replaced. + ## Demo ``` bash @@ -39,6 +54,7 @@ hexo migrate github-issue Yikun/yikun.github.com It will migrate Yikun/yikun.github.com issue to hexo. ## History +- 0.1.6 add original front-matter support & remove top label support - 0.1.5 add --publish support - 0.1.4 category/top/draft support - 0.1.3 issue number support diff --git a/index.js b/index.js index 31a9f7c..103e688 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ var github = require('octonode'); +var matter = require('hexo-front-matter'); var log = hexo.log, post = hexo.post, @@ -31,8 +32,9 @@ hexo.extend.migrator.register('github-issue', function(args, callback){ }); function nextpage(cb) { - var topPrefix = 'top_'; - var categoryPrefix = 'category_'; + var category_prefix = 'category_'; + // refrence from https://github.com/hexojs/hexo-front-matter/blob/69516870249e91ba3e77e5b2e395645b3991d97a/lib/front_matter.js#L5 + var regexp = /^(-{3,})(\r\n)([\s\S]+?)\r\n\1\r\n?([\s\S]*)/; repo.issues(pagesn, function(err, body, headers) { if (!err) { if (body && body.length) { @@ -45,12 +47,9 @@ function nextpage(cb) { for (var i in issue.labels) { var name = issue.labels[i].name; - if (name.indexOf(categoryPrefix) != -1) { - name = name.substr(categoryPrefix.length); + if (name.indexOf(category_prefix) != -1) { + name = name.substr(category_prefix.length); categories.push(name); - } else if (name.indexOf(topPrefix) != -1) { - name = name.substr(topPrefix.length); - data.top = parseInt(name); } else if (name.toLowerCase() == "draft") { data.layout = "draft" } else if (name.toLowerCase() == "publish") { @@ -60,17 +59,33 @@ function nextpage(cb) { } } - data.title = issue.title.replace(/\"/g,""); + // parse front-matter + var match = issue.body.match(regexp); + if (match) { + // replace CRLF with LF before parse + var separator = match[1]; + var frontMatterData = match[3].replace(/\r\n/g, '\n'); + var content = match[4]; + var issueBody = separator + '\n' + frontMatterData + '\n' + separator + '\n' + content; + } else { + var issueBody = issue.body; + } + var { _content, ...meta } = matter.parse(issueBody); + + data.title = (meta.title ? meta.title : issue.title).replace(/\"/g,""); // if you migrate with --publish option, will skip unpublished issue if (publish_mode && (!published_tag) ) { log.i('skip unpublished post: ' + data.title); continue; } - data.content = issue.body; + + data.content = _content; data.date = issue.created_at; data.tags = tags; data.categories = categories; data.number = issue.number; + data = Object.assign(data, meta); + post.create(data, true); log.i('create post: ' + data.title); } diff --git a/package.json b/package.json index 24509b6..c3ff351 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "hexo-migrator-github-issue", - "version": "0.1.5", + "version": "0.1.6", "description": "Github issue migrator plugin for Hexo.", "main": "index.js", "dependencies": { - "octonode": "^0.6.16" + "octonode": "^0.6.16", + "hexo-front-matter": "*" }, "repository": { "type": "git",