-
Notifications
You must be signed in to change notification settings - Fork 4
Support front-matter in the issue content #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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_'; | ||
Yikun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 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]*)/; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个regexp自己写的吗?靠谱不,看的头大。:)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这一段主要是将FrontMatter中的CRLF替换为LF(我的文章中都是CRLF,不知道你的有没有这个问题),不然无法正常解析。如果全文替换,又会影响文章生成,所以最后用了正则。
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 加个注释吧
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 或者是不是可以直接引用这个变量,防止发生变化。加个注释和链接也可以,你自己看吧。:)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
正则稍有不同, |
||
| 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); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| { | ||
| "name": "hexo-migrator-github-issue", | ||
| "version": "0.1.5", | ||
| "version": "0.1.6", | ||
| "description": "Github issue migrator plugin for Hexo.", | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "version": "0.1.5" 直接改成0.16吧,merge后我就release |
||
| "main": "index.js", | ||
| "dependencies": { | ||
| "octonode": "^0.6.16" | ||
| "octonode": "^0.6.16", | ||
| "hexo-front-matter": "*" | ||
Yikun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Front-matter support
If you specify the front-matter in the issue content, like:
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.Front-matter作为一个独立的章节写吧