From c38583def3f9ec2e63167d257c92a47f007180b1 Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Mon, 21 Oct 2019 23:56:51 +0800 Subject: [PATCH] Initial commit --- .eslintignore | 3 ++ .eslintrc.json | 20 +++++++++ .gitignore | 62 ++++++++++++++++++++++++++ .travis.yml | 21 +++++++++ LICENSE | 21 +++++++++ README.md | 79 ++++++++++++++++++++++++++++++++++ lib/index.js | 28 ++++++++++++ lib/new2.js | 47 ++++++++++++++++++++ lib/unique_post_path_filter.js | 9 ++++ package.json | 50 +++++++++++++++++++++ test/index.js | 0 11 files changed, 340 insertions(+) create mode 100644 .eslintignore create mode 100644 .eslintrc.json create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 lib/index.js create mode 100644 lib/new2.js create mode 100644 lib/unique_post_path_filter.js create mode 100644 package.json create mode 100644 test/index.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..f6b9638 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +node_modules/ +coverage/ +tmp/ \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..ad143a9 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,20 @@ +{ + "extends": "hexo", + "root": true, + "globals": { + "hexo": true + }, + "rules": { + "one-var": 0, + "operator-linebreak": [ + 2, + "after" + ], + "comma-spacing": [ + 2, + { + "after": true + } + ] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49b97c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next +package-lock.json diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e78421b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,21 @@ +language: node_js + +sudo: false + +cache: + apt: true + directories: + - node_modules + +node_js: + - "8" + - "10" + - "node" + +script: + - npm run eslint + - npm run test-cov + +after_script: + - npm install coveralls + - nyc report --reporter=text-lcov | coveralls diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..17304de --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 dailyrandomphoto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b393d13 --- /dev/null +++ b/README.md @@ -0,0 +1,79 @@ +# hexo-unique-post-path + +[![NPM Version][npm-version-image]][npm-url] +[![NPM Downloads][npm-downloads-image]][npm-url] +[![LICENSE][license-image]][license-url] +[![Build Status][travis-image]][travis-url] +[![Coverage Status][coveralls-image]][coveralls-url] +[![dependencies Status][dependencies-image]][dependencies-url] +[![devDependencies Status][devDependencies-image]][devDependencies-url] + +This plug-in helps Hexo create new posts with unique auto-generated paths. + + +## Installation + +```sh +npm install --save hexo-unique-post-path +``` + +## Usages + +There are two ways to use this plug-in to create new posts with unique auto-generated paths. + +### 1. Use `hexo new2` command +```sh +hexo new2 [layout] [title] +``` + +e.g. +```sh +$ hexo new2 + +INFO Created: ./source/_posts/ck20kqmij0001ieyn4es62xh7.md +``` + +The `title` argument can be omitted and its default value is `' '`. + +You can define the default value of `title` via `unique_post_path` options in the `_config.yml` file. + +``` +#_cofig.yml + +unique_post_path: + title_default: "new post" +``` + +### 2. Use `hexo new` command with `unique_post_path` configuration + +Add `unique_post_path` options to the `_config.yml` file. +``` +#_cofig.yml + +unique_post_path: + auto: true +``` + +Then use `new` command as before. +``` +$ hexo new "My New Post" + +INFO Created: ./source/_posts/ck20kqmij0001ieyn4es62xh7.md +``` +## License +Copyright (c) 2019 dailyrandomphoto. Licensed under the [MIT license][license-url]. + +[npm-url]: https://www.npmjs.com/package/hexo-unique-post-path +[travis-url]: https://travis-ci.org/dailyrandomphoto/hexo-unique-post-path +[coveralls-url]: https://coveralls.io/github/dailyrandomphoto/hexo-unique-post-path?branch=master +[license-url]: LICENSE +[dependencies-url]: https://david-dm.org/dailyrandomphoto/hexo-unique-post-path +[devDependencies-url]: https://david-dm.org/dailyrandomphoto/hexo-unique-post-path?type=dev + +[npm-downloads-image]: https://img.shields.io/npm/dm/hexo-unique-post-path.svg +[npm-version-image]: https://img.shields.io/npm/v/hexo-unique-post-path.svg +[license-image]: https://img.shields.io/npm/l/hexo-unique-post-path.svg +[travis-image]: https://img.shields.io/travis/dailyrandomphoto/hexo-unique-post-path/master +[coveralls-image]: https://coveralls.io/repos/github/dailyrandomphoto/hexo-unique-post-path/badge.svg?branch=master +[dependencies-image]: https://david-dm.org/dailyrandomphoto/hexo-unique-post-path/status.svg +[devDependencies-image]: https://david-dm.org/dailyrandomphoto/hexo-unique-post-path/dev-status.svg diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..4ee21ca --- /dev/null +++ b/lib/index.js @@ -0,0 +1,28 @@ +'use strict'; + +(function(ctx) { + const { config, extend } = ctx; + const { console, filter } = extend; + const myconfig = Object.assign({ + auto: false, + title_default: ' ' + }, config.unique_post_path); + + console.register('new2', 'Create a new post with a auto generated unique path.', { + usage: '[layout] [title]', + arguments: [ + {name: 'layout', desc: 'Post layout. Use post, page, draft or whatever you want.'}, + {name: 'title', desc: 'Post title. Wrap it with quotations to escape.'} + ], + options: [ + {name: '-r, --replace', desc: 'Replace the current post if existed.'}, + {name: '-s, --slug', desc: 'Post slug. Customize the URL of the post.'} + ] + }, require('./new2')(myconfig)); + + if (myconfig.auto) { + // register a filter with highest priority. + filter.register('new_post_path', require('./unique_post_path_filter'), 1); + } + +}(hexo)); diff --git a/lib/new2.js b/lib/new2.js new file mode 100644 index 0000000..6a2bc5a --- /dev/null +++ b/lib/new2.js @@ -0,0 +1,47 @@ +'use strict'; + +const tildify = require('tildify'); // eslint-disable-line node/no-missing-require,node/no-extraneous-require +const chalk = require('chalk'); // eslint-disable-line node/no-missing-require,node/no-extraneous-require +const cuid = require('cuid'); + +const reservedKeys = { + _: true, + title: true, + layout: true, + slug: true, + path: true, + replace: true, + // Global options + config: true, + debug: true, + safe: true, + silent: true +}; + +module.exports = function(config) { + + return function newConsole(args) { + // Display help message if user didn't input any arguments + // if (!args._.length) { + // return this.call('help', {_: ['new2']}); + // } + + const data = { + title: args._.pop() || config.title_default, + layout: args._.length ? args._[0] : this.config.default_layout, + slug: args.s || args.slug, + path: cuid() + }; + + const keys = Object.keys(args); + + for (let i = 0, len = keys.length; i < len; i++) { + const key = keys[i]; + if (!reservedKeys[key]) data[key] = args[key]; + } + + return this.post.create(data, args.r || args.replace).then(post => { + this.log.info('Created: %s', chalk.magenta(tildify(post.path))); + }); + }; +}; diff --git a/lib/unique_post_path_filter.js b/lib/unique_post_path_filter.js new file mode 100644 index 0000000..097885b --- /dev/null +++ b/lib/unique_post_path_filter.js @@ -0,0 +1,9 @@ +'use strict'; + +const cuid = require('cuid'); + +module.exports = function(data, replace) { + // set path value as a value of cuid. + data.path = cuid(); + return data; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..14a227f --- /dev/null +++ b/package.json @@ -0,0 +1,50 @@ +{ + "name": "hexo-unique-post-path", + "version": "0.0.1", + "description": "This plug-in helps Hexo create new posts with unique auto-generated paths.", + "main": "lib/index.js", + "scripts": { + "eslint": "eslint .", + "test": "mocha test/index.js", + "test-cov": "nyc npm run test" + }, + "dependencies": { + "chalk": "^2.4.2", + "cuid": "^2.1.6", + "tildify": "^2.0.0" + }, + "devDependencies": { + "chai": "^4.2.0", + "eslint": "^6.5.1", + "eslint-config-hexo": "^3.0.0", + "mocha": "^6.2.1", + "nyc": "^14.1.1" + }, + "keywords": [ + "hexo", + "blog", + "cuid", + "id", + "generate", + "plugin", + "extension" + ], + "files": [ + "lib/", + "LICENSE", + "README.md" + ], + "engines": { + "node": ">=8.6.0" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/dailyrandomphoto/hexo-unique-post-path.git" + }, + "author": "dailyrandomphoto (https://www.dailyrandomphoto.com/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/dailyrandomphoto/hexo-unique-post-path/issues" + }, + "homepage": "https://github.com/dailyrandomphoto/hexo-unique-post-path#readme" +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..e69de29