From 2f3aa1e9029828cd628bc837a0eb6c123f1d1a0a Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sun, 24 Feb 2019 20:06:01 +0800 Subject: [PATCH 1/3] plugin-clean-urls --- .../@vuepress/plugin-clean-urls/.npmignore | 3 +++ .../@vuepress/plugin-clean-urls/README.md | 5 ++++ packages/@vuepress/plugin-clean-urls/index.js | 16 ++++++++++++ .../@vuepress/plugin-clean-urls/package.json | 26 +++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 packages/@vuepress/plugin-clean-urls/.npmignore create mode 100644 packages/@vuepress/plugin-clean-urls/README.md create mode 100644 packages/@vuepress/plugin-clean-urls/index.js create mode 100644 packages/@vuepress/plugin-clean-urls/package.json diff --git a/packages/@vuepress/plugin-clean-urls/.npmignore b/packages/@vuepress/plugin-clean-urls/.npmignore new file mode 100644 index 0000000000..13c38ea313 --- /dev/null +++ b/packages/@vuepress/plugin-clean-urls/.npmignore @@ -0,0 +1,3 @@ +__tests__ +__mocks__ +.temp diff --git a/packages/@vuepress/plugin-clean-urls/README.md b/packages/@vuepress/plugin-clean-urls/README.md new file mode 100644 index 0000000000..f9bfc3dfbb --- /dev/null +++ b/packages/@vuepress/plugin-clean-urls/README.md @@ -0,0 +1,5 @@ +# @vuepress/plugin-clean-urls + +> clean urls plugin for vuepress + +See [documentation](https://vuepress.vuejs.org/plugin/official/plugin-clean-urls.html). diff --git a/packages/@vuepress/plugin-clean-urls/index.js b/packages/@vuepress/plugin-clean-urls/index.js new file mode 100644 index 0000000000..6f67f01759 --- /dev/null +++ b/packages/@vuepress/plugin-clean-urls/index.js @@ -0,0 +1,16 @@ +module.exports = (options = {}, context) => { + const { + normalSuffix = '', + indexSuffix = '/' + } = options + + return { + extendPageData (page) { + const { regularPath, frontmatter = {}} = page + if (frontmatter.permalink) return + page.path = regularPath + .replace(/\.html$/, normalSuffix) + .replace(/\/$/, indexSuffix) + } + } +} diff --git a/packages/@vuepress/plugin-clean-urls/package.json b/packages/@vuepress/plugin-clean-urls/package.json new file mode 100644 index 0000000000..f678b34391 --- /dev/null +++ b/packages/@vuepress/plugin-clean-urls/package.json @@ -0,0 +1,26 @@ +{ + "name": "@vuepress/plugin-clean-urls", + "version": "1.0.0-alpha.39", + "description": "clean urls plugin for vuepress", + "main": "index.js", + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/vuejs/vuepress.git", + "directory": "packages/@vuepress/plugin-clean-urls" + }, + "keywords": [ + "documentation", + "vue", + "vuepress", + "generator" + ], + "author": "Shigma <1700011071@pku.edu.cn>", + "license": "MIT", + "bugs": { + "url": "https://github.com/vuejs/vuepress/issues" + }, + "homepage": "https://github.com/vuejs/vuepress/packages/@vuepress/plugin-clean-urls#readme" +} From 36ad3d74269ecf48987a2ee39eb1407fb516b69c Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sun, 24 Feb 2019 20:55:19 +0800 Subject: [PATCH 2/3] add docs --- packages/docs/docs/.vuepress/config.js | 1 + .../docs/plugin/official/plugin-clean-urls.md | 51 +++++++++++++++++++ .../zh/plugin/official/plugin-clean-urls.md | 51 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 packages/docs/docs/plugin/official/plugin-clean-urls.md create mode 100644 packages/docs/docs/zh/plugin/official/plugin-clean-urls.md diff --git a/packages/docs/docs/.vuepress/config.js b/packages/docs/docs/.vuepress/config.js index 624e35d2d8..710c2313c4 100644 --- a/packages/docs/docs/.vuepress/config.js +++ b/packages/docs/docs/.vuepress/config.js @@ -143,6 +143,7 @@ function getPluginSidebar (pluginTitle, pluginIntro, officialPluginTitle) { 'official/plugin-medium-zoom', 'official/plugin-back-to-top', 'official/plugin-register-components', + 'official/plugin-clean-urls' ] } ] diff --git a/packages/docs/docs/plugin/official/plugin-clean-urls.md b/packages/docs/docs/plugin/official/plugin-clean-urls.md new file mode 100644 index 0000000000..6f801be040 --- /dev/null +++ b/packages/docs/docs/plugin/official/plugin-clean-urls.md @@ -0,0 +1,51 @@ +--- +title: clean-urls +metaTitle: A plugin of automatically generating clean urls | VuePress +--- + +# [@vuepress/plugin-clean-urls](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/plugin-clean-urls) + +> A plugin of automatically generating clean urls + +## Install + +```bash +yarn add -D @vuepress/plugin-clean-urls +# OR npm install -D @vuepress/plugin-clean-urls +``` + +## Usage + +```javascript +module.exports = { + plugins: ['@vuepress/clean-urls'] +} +``` + +## Options + +### normalSuffix + +- Type: `string` +- Default: `''` + +The suffix for normal pages. For example, `foo/bar.md` will become: + +- `foo/bar.html` by default (without this plugin) +- `foo/bar/` (with `normalSuffix` set to `'/'`) +- `foo/bar` (with `normalSuffix` set to `''`) + +### indexSuffix + +- Type: `string` +- Default: `'/'` + +The suffix for index pages. For example, `foo/index.md` will become: + +- `foo/` by default (without this plugin) +- `foo` (with `indexSuffix` set to `''`) +- `foo/index.html` (with `indexSuffix` set to `'/index.html'`) + +::: tip +An index page is a page with a file name of index.md or readme.md (case insensitive). +::: diff --git a/packages/docs/docs/zh/plugin/official/plugin-clean-urls.md b/packages/docs/docs/zh/plugin/official/plugin-clean-urls.md new file mode 100644 index 0000000000..e986d188ef --- /dev/null +++ b/packages/docs/docs/zh/plugin/official/plugin-clean-urls.md @@ -0,0 +1,51 @@ +--- +title: clean-urls +metaTitle: 自动生成简洁链接的插件 | VuePress +--- + +# [@vuepress/plugin-clean-urls](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/plugin-clean-urls) + +> 自动生成简洁链接的插件 + +## 安装 + +```bash +yarn add -D @vuepress/plugin-clean-urls +# OR npm install -D @vuepress/plugin-clean-urls +``` + +## 使用 + +```javascript +module.exports = { + plugins: ['@vuepress/clean-urls'] +} +``` + +## 选项 + +### normalSuffix + +- 类型: `string` +- 默认值: `''` + +普通页面的链接后缀。举个例子,`foo/bar.md` 会自动变成: + +- `foo/bar.html` 在默认情况下(未安装本插件时) +- `foo/bar/`(当 `normalSuffix` 被设为 `'/'` 时) +- `foo/bar`(当 `normalSuffix` 被设为 `''` 时) + +### indexSuffix + +- 类型: `string` +- 默认值: `'/'` + +索引页面的链接后缀。举个例子,`foo/index.md` 会自动变成: + +- `foo/` 在默认情况下(未安装本插件时) +- `foo`(当 `indexSuffix` 被设为 `''` 时) +- `foo/index.html`(当 `indexSuffix` 被设为 `'/index.html'` 时) + +::: tip +索引页面是指文件名为 index.md 或者 readme.md 的页面(不区分大小写)。 +::: From 7e0915fa9ae4172caba7eabad147b32c683a1c7b Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 25 Feb 2019 10:30:51 +0800 Subject: [PATCH 3/3] Update index.js --- packages/@vuepress/plugin-clean-urls/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/@vuepress/plugin-clean-urls/index.js b/packages/@vuepress/plugin-clean-urls/index.js index 6f67f01759..d09d0754e5 100644 --- a/packages/@vuepress/plugin-clean-urls/index.js +++ b/packages/@vuepress/plugin-clean-urls/index.js @@ -8,9 +8,15 @@ module.exports = (options = {}, context) => { extendPageData (page) { const { regularPath, frontmatter = {}} = page if (frontmatter.permalink) return - page.path = regularPath - .replace(/\.html$/, normalSuffix) - .replace(/\/$/, indexSuffix) + if (regularPath.endsWith('.html')) { + // normal path + // e.g. foo/bar.md -> foo/bar.html + page.path = regularPath.slice(0, -5) + normalSuffix + } else if (regularPath.endsWith('/')) { + // index path + // e.g. foo/index.md -> foo/ + page.path = regularPath.slice(0, -1) + indexSuffix + } } } }