From e56f8f198c33e030bca9192e8cd0662bdc0ddd0b Mon Sep 17 00:00:00 2001 From: liuyc Date: Wed, 2 Jun 2021 18:48:38 +0800 Subject: [PATCH 1/4] Support front-matter in the issue content --- index.js | 13 ++++++++++++- package.json | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 31a9f7c..da45444 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, @@ -60,17 +61,27 @@ function nextpage(cb) { } } + // parse front-matter + issue.body = issue.body.replace(/\r\n/g, '\n'); + var { _content, ...meta } = matter.parse(issue.body); + data.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; + + if (meta) { + data = Object.assign(meta, data); + } + post.create(data, true); log.i('create post: ' + data.title); } diff --git a/package.json b/package.json index 24509b6..52c7342 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "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", From 93d17942deba47da9ad0913490c0e7b53f4161aa Mon Sep 17 00:00:00 2001 From: liuyc Date: Thu, 3 Jun 2021 09:38:06 +0800 Subject: [PATCH 2/4] Remove label support for top_x --- index.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index da45444..c2c554f 100644 --- a/index.js +++ b/index.js @@ -32,8 +32,7 @@ hexo.extend.migrator.register('github-issue', function(args, callback){ }); function nextpage(cb) { - var topPrefix = 'top_'; - var categoryPrefix = 'category_'; + var category_prefix = 'category_'; repo.issues(pagesn, function(err, body, headers) { if (!err) { if (body && body.length) { @@ -46,12 +45,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") { @@ -77,10 +73,7 @@ function nextpage(cb) { data.tags = tags; data.categories = categories; data.number = issue.number; - - if (meta) { - data = Object.assign(meta, data); - } + data = Object.assign(meta, data); post.create(data, true); log.i('create post: ' + data.title); From 251c8b7961939af2f9f4bca0940cb345ed7bb0c8 Mon Sep 17 00:00:00 2001 From: liuyc Date: Thu, 3 Jun 2021 15:15:02 +0800 Subject: [PATCH 3/4] Fix parse front-matter --- README.md | 14 +++++++++++++- index.js | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 35fc44d..060e2b0 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,21 @@ $ 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. +YAML Front-matter in the issue content is also supported: + +``` +--- +cover: http://demo.jpeg +top: 1 +--- + +# Title +Hello World! +``` + ## Demo ``` bash @@ -39,6 +50,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 c2c554f..fd86745 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ hexo.extend.migrator.register('github-issue', function(args, callback){ function nextpage(cb) { var category_prefix = 'category_'; + 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) { @@ -58,9 +59,17 @@ function nextpage(cb) { } // parse front-matter - issue.body = issue.body.replace(/\r\n/g, '\n'); - var { _content, ...meta } = matter.parse(issue.body); - + var match = issue.body.match(regexp); + if (match) { + 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 = issue.title.replace(/\"/g,""); // if you migrate with --publish option, will skip unpublished issue if (publish_mode && (!published_tag) ) { From de36c2b2903aee6145cc6e1801d27d670ceee228 Mon Sep 17 00:00:00 2001 From: liuyc Date: Thu, 3 Jun 2021 20:54:06 +0800 Subject: [PATCH 4/4] Meta overwrite label --- README.md | 8 ++++++-- index.js | 6 ++++-- package.json | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 060e2b0..346423a 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,22 @@ It will migrate owner/repo issue to hexo, there are some specific issue label is - "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. -YAML Front-matter in the issue content is also supported: +### 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 diff --git a/index.js b/index.js index fd86745..103e688 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ hexo.extend.migrator.register('github-issue', function(args, callback){ function nextpage(cb) { 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) { @@ -61,6 +62,7 @@ function nextpage(cb) { // 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]; @@ -70,7 +72,7 @@ function nextpage(cb) { } var { _content, ...meta } = matter.parse(issueBody); - data.title = issue.title.replace(/\"/g,""); + 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); @@ -82,7 +84,7 @@ function nextpage(cb) { data.tags = tags; data.categories = categories; data.number = issue.number; - data = Object.assign(meta, data); + data = Object.assign(data, meta); post.create(data, true); log.i('create post: ' + data.title); diff --git a/package.json b/package.json index 52c7342..c3ff351 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexo-migrator-github-issue", - "version": "0.1.5", + "version": "0.1.6", "description": "Github issue migrator plugin for Hexo.", "main": "index.js", "dependencies": {