Skip to content

Commit

Permalink
Merge pull request #63 from hyliang96/master
Browse files Browse the repository at this point in the history
support `.textbundle` file format and solve issue #56
  • Loading branch information
rozbo committed Feb 12, 2022
2 parents 93b7b40 + 52e6574 commit e9c5ee9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 97 deletions.
11 changes: 8 additions & 3 deletions README.md
Expand Up @@ -3,7 +3,10 @@
[![npm](https://img.shields.io/npm/dy/hexo-abbrlink.svg)](https://www.npmjs.com/package/hexo-abbrlink)
[![npm](https://img.shields.io/npm/dt/hexo-abbrlink.svg)](https://www.npmjs.com/package/hexo-abbrlink)

A [Hexo plugin](https://hexo.io/plugins/) to generate static post link based on post titles.
A [Hexo plugin](https://hexo.io/plugins/) to generate static post link based on title and data in the post front.

This plugin supports `.textbundle` -- a file format contents markdown and its assets. Actually, `.textbundle` file is a folder which shows like a file in Finder on macOS.

## Suggest
[https://github.com/rozbo/hexo-abbrlink2](https://github.com/rozbo/hexo-abbrlink2), supports the orderly growth of ID is beta now.
The working principle of `hexo-abbrlink2` is different from this plug-in, not as an upgraded version of this plugin, they are different individuals.
Expand All @@ -20,7 +23,9 @@ npm install hexo-abbrlink --save
Modify permalink in config.yml file:

```
permalink: posts/:abbrlink/
permalink: posts/:abbrlink/
# or
permalink: posts/:abbrlink.html
```

There are two settings:
Expand All @@ -44,7 +49,7 @@ abbrlink:
over_write: false
auto_title: false #enable auto title, it can auto fill the title by path
auto_date: false #enable auto date, it can auto fill the date by time today
force: false #enable force mode,in this mode, the plugin will ignore the cache, and calc the abbrlink for every post even it already had abbrlink.
force: false #enable force mode,in this mode, the plugin will ignore the cache, and calc the abbrlink for every post even it already had abbrlink. This only updates abbrlink rather than other front variables.
```

## Sample
Expand Down
192 changes: 98 additions & 94 deletions lib/logic.js
Expand Up @@ -32,110 +32,114 @@ let logic = function (data) {
} else {
abbrlink = org_get_abbrlink(data).abbrlink;
}
if (!abbrlink || config.force) {
var opt_alg = this.config.abbrlink && this.config.abbrlink.alg ? this.config.abbrlink.alg : 'crc16';
var opt_rep = this.config.abbrlink && this.config.abbrlink.rep ? this.config.abbrlink.rep : 'dec';
// if (!abbrlink || abbrlink == '0' || config.force) {
let root_path = data.source.startsWith('_drafts/') ? 'source/_drafts' : 'source/_posts'

let res = opt_alg == 'crc32' ? crc32.str(data.title) >>> 0 : crc16(data.title) >>> 0;
//check this abbrlink is already exist then get a different one
abbrlink = model.check(res);
//set abbrlink to hex or dec
abbrlink = opt_rep == 'hex' ? abbrlink.toString(16) : abbrlink;
data.abbrlink = abbrlink;
let postStr;
if (!/.*\.org/.test(data.source)) {
//re parse front matter
var tmpPost = front.parse(data.raw);
//add new generated link
tmpPost.abbrlink = abbrlink;
//re parse front matter
var tmpPost = front.parse(data.raw);

// ------ auto title ?
if (this.config.abbrlink && this.config.abbrlink.auto_title && !tmpPost.title) {
// maybe the title is path/to/something.md
// so we split / first and split . again
const pathParts = data.source.split('/');
let last = pathParts[pathParts.length - 1];
const endPort = last.indexOf('.');
if (endPort > -1) {
last = last.substring(0, endPort);
}
tmpPost.title = last;
// ------ auto title ?
if (this.config.abbrlink && this.config.abbrlink.auto_title && !tmpPost.title) {
// maybe the title is path/to/something.md
// so we split / first and split . again
const pathParts = data.source.split('/');
let last = pathParts[pathParts.length - 1];
let last2 = pathParts[pathParts.length - 2];

log.i('Generated: title [%s] for post [ %s ]', tmpPost.title, data.full_source);
const endPort2 = last2.lastIndexOf('.');
var last2tail = ''
var last2front = ''
if (endPort2 > -1) {
last2tail = last2.substring(endPort2 + 1);
last2front = last2.substring(0, endPort2);
}
if (last2tail == 'textbundle'){
tmpPost.title = last2front;
} else {
const endPort = last.indexOf('.');
if (endPort > -1) {
last = last.substring(0, endPort);
}
// ----- auto date ? easy
if (this.config.abbrlink && this.config.abbrlink.auto_date && !tmpPost.date) {
tmpPost.date = data.date.format('YYYY-MM-DD HH:mm:ss');
log.i('Generated: date [%s] for post [ %s ]', tmpPost.date, data.full_source);
tmpPost.title = last;
}
if (data.title.length == 0)
log.i('No title [%s] in post [ %s ]', data.title, data.full_source);
log.i('Generated: title [%s] for post [ %s ]', tmpPost.title, data.full_source);
}

// ----- auto date ? easy
if (this.config.abbrlink && this.config.abbrlink.auto_date && !tmpPost.date) {
tmpPost.date = data.date.format('YYYY-MM-DD HH:mm:ss');
log.i('Generated: date [%s] for post [ %s ]', tmpPost.date, data.full_source);
}

//From: hexo-auto-category
//see:https://github.com/xu-song/hexo-auto-category
//File: hexo-auto-category\lib\logic.js
var opt_AutoCategoryEnable = config.auto_category && config.auto_category.enable;
var overwrite = config.auto_category && config.auto_category.over_write;
if (opt_AutoCategoryEnable && overwrite) {
var categories = data.source.split('/');

if (categories.length - 2 >= 0) {
let last2 = categories[categories.length - 2];
const endPort2 = last2.lastIndexOf('.');
var last2tail = ''
var last2front = ''
if (endPort2 > -1) {
last2tail = last2.substring(endPort2 + 1);
last2front = last2.substring(0, endPort2);
}
//+++
//From: hexo-auto-category
//see:https://github.com/xu-song/hexo-auto-category
//File: hexo-auto-category\lib\logic.js
var opt_AutoCategoryEnable = config.auto_category && config.auto_category.enable;
var overwrite = config.auto_category && config.auto_category.over_write;
if (opt_AutoCategoryEnable && overwrite) {
var categories = data.source.split('/');
var opt_AutoCategoryDepth = config.auto_category.depth || 3;
var depth = opt_AutoCategoryDepth || categories.length - 2;
if (categories.length - 2 == 0 || depth == 0) {
tmpPost.categories = this.config.default_category;
} else {
var newCategories = categories.slice(1, 1 + Math.min(depth, categories.length - 2));
//prevents duplicate file changes
if (
!Array.isArray(tmpPost.categories) ||
!tmpPost.categories.join('_') == newCategories.join('_')
) {
tmpPost.categories = newCategories;
}
}
if (last2tail == 'textbundle'){
categories.pop();
}
//+++
//process post
postStr = front.stringify(tmpPost);
postStr = '---\n' + postStr;
fs.writeFileSync(data.full_source, postStr, 'utf-8');
} else {
postStr = data.raw.split('\n');
postStr.splice(2, 0, '#+ABBRLINK: ' + abbrlink);
fs.writeFileSync(data.full_source, postStr.join('\n'), 'utf-8');
}
if (data.source.startsWith('_drafts/')) {
// is draft //
if (data.title.length == 0)
log.i('No title found for draft [source/_drafts/%s.md][ %s ]', data.slug, data.title);
log.i('Generate link [%s] for draft [source/_drafts/%s.md][ %s ]', abbrlink, data.slug, data.title);
//+++
//From: hexo-auto-category
//see:https://github.com/xu-song/hexo-auto-category
//File: hexo-auto-category\lib\logic.js
log.i(
'Generated: categories [%s] for draft [source/_drafts/%s.md][ %s ]',
tmpPost.categories,
data.slug,
data.title
);
//+++

var opt_AutoCategoryDepth = config.auto_category.depth || 3;
var depth = opt_AutoCategoryDepth || categories.length - 2;
if (categories.length - 2 == 0 || depth == 0) {
tmpPost.categories = this.config.default_category;
} else {
// is not draft //
if (data.title.length == 0)
log.i('No title found for post [source/_posts/%s.md][ %s ]', data.slug, data.title);
log.i('Generate link [%s] for post [source/_posts/%s.md][ %s ]', abbrlink, data.slug, data.title);
//+++
//From: hexo-auto-category
//see:https://github.com/xu-song/hexo-auto-category
//File: hexo-auto-category\lib\logic.js
log.i(
'Generated: categories [%s] for post [source/_posts/%s.md][ %s ]',
tmpPost.categories,
data.slug,
data.title
);
//+++
var newCategories = categories.slice(1, 1 + Math.min(depth, categories.length - 2));
//prevents duplicate file changes
if (
!Array.isArray(tmpPost.categories) ||
tmpPost.categories.join('_') != newCategories.join('_')
) {
tmpPost.categories = newCategories;
log.i('Generated: categories [%s] for post [ %s ]', tmpPost.categories, data.full_source);
}
}
}
model.add(abbrlink);

//add new generated link
if (!abbrlink || abbrlink == '0' || config.force) {
var opt_alg = this.config.abbrlink && this.config.abbrlink.alg ? this.config.abbrlink.alg : 'crc16';
var opt_rep = this.config.abbrlink && this.config.abbrlink.rep ? this.config.abbrlink.rep : 'dec';
let res = opt_alg == 'crc32' ? crc32.str(tmpPost.title + tmpPost.date) >>> 0 : crc16(tmpPost.title + tmpPost.date) >>> 0;
//check this abbrlink is already exist then get a different one
abbrlink = model.check(res);
//set abbrlink to hex or dec
abbrlink = opt_rep == 'hex' ? abbrlink.toString(16) : abbrlink;
data.abbrlink = abbrlink;
tmpPost.abbrlink = abbrlink;
log.i('Generated: link [%s] for post [ %s ]', tmpPost.abbrlink, data.full_source);
}
let abbrlink_int = opt_rep == 'hex' ? parseInt('0x'+abbrlink) : abbrlink
model.add(abbrlink_int);

let postStr;
if (!/.*\.org/.test(data.source)) {
//process post
postStr = front.stringify(tmpPost);
postStr = '---\n' + postStr;
fs.writeFileSync(data.full_source, postStr, 'utf-8');
} else {
postStr = data.raw.split('\n');
postStr.splice(2, 0, '#+ABBRLINK: ' + abbrlink);
fs.writeFileSync(data.full_source, postStr.join('\n'), 'utf-8');
}

}
return data;
};
Expand Down

0 comments on commit e9c5ee9

Please sign in to comment.