Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Owner

@Yikun Yikun Jun 3, 2021

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:

---
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.

Front-matter作为一个独立的章节写吧

### 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
Expand All @@ -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
Expand Down
33 changes: 24 additions & 9 deletions index.js
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,
Expand Down Expand Up @@ -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]*)/;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个regexp自己写的吗?靠谱不,看的头大。:)

Copy link
Contributor Author

@CloudyCity CloudyCity Jun 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@CloudyCity CloudyCity Jun 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一段主要是将FrontMatter中的CRLF替换为LF(我的文章中都是CRLF,不知道你的有没有这个问题),不然无法正常解析。如果全文替换,又会影响文章生成,所以最后用了正则。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个注释吧

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

或者是不是可以直接引用这个变量,防止发生变化。加个注释和链接也可以,你自己看吧。:)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

或者是不是可以直接引用这个变量,防止发生变化。加个注释和链接也可以,你自己看吧。:)

正则稍有不同,hexo-front-matter用的是LF,这里是用CRLF。我注释个链接。

repo.issues(pagesn, function(err, body, headers) {
if (!err) {
if (body && body.length) {
Expand All @@ -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") {
Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
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.",
Copy link
Owner

Choose a reason for hiding this comment

The 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": "*"
},
"repository": {
"type": "git",
Expand Down