Skip to content
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

ref: using pangu.js for chinese documentation lint #4656

Merged
merged 11 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
26 changes: 26 additions & 0 deletions docs/.format/chineseFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const file = require('./file');
const width = require('string-width');
const remark = require('remark');
const pangu = require('remark-pangu');
const frontmatter = require('remark-frontmatter');

const prettier = require('remark-preset-prettier');

module.exports = {
rules: (list) => list.filter((e) => e.lang === file.LANG_CN),
handler: async (doc) => {
let result = await remark()
.use(frontmatter)
.use(pangu, {
inlineCode: false,
})
.use(prettier)
.use({
settings: {
stringLength: width,
},
})
.process(doc);
return typeof result === 'string' ? result : typeof result.contents === 'string' ? result.contents : result.result;
},
};
11 changes: 11 additions & 0 deletions docs/.format/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs');

module.exports = {
ROUTE_TYPE: 'route',
GUIDE_TYPE: 'guide',
NAV_TYPE: 'nav',
LANG_CN: 'zh-CN',
LANG_EN: 'en-US',
readFile: async (filePath) => fs.promises.readFile(filePath, { encoding: 'utf8' }),
writeFile: async (filePath, data) => fs.promises.writeFile(filePath, data, { encoding: 'utf8' }),
};
115 changes: 115 additions & 0 deletions docs/.format/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const file = require('./file');
const sgf = require('staged-git-files');
const path = require('path');
const sortByHeading = require('./sortByHeading');
const chineseFormat = require('./chineseFormat');

/**
* Processors are objects contains two methods:
* `rules(list)`, and `handler(str)`
* rules filters required file document object
* and handler get document string and return formatted document
*/
const processors = [sortByHeading, chineseFormat];

// Helpers
const loopSideBar = (children, type, lang, prefix) =>
children
.filter((e) => e !== '')
.map((x) => ({
path: path.resolve(__dirname, '..', prefix, `./${x}.md`),
type,
lang,
}));
const loopNav = (nav, lang) =>
nav.map((e) => ({
path: path.resolve(__dirname, '..', e.link.slice(1), 'README.md'),
type: file.NAV_TYPE,
lang,
}));
const loopType = (sidebar, lang, prefix) => loopSideBar(sidebar[0].children, file.GUIDE_TYPE, lang, prefix).concat(loopSideBar(sidebar[1].children, file.ROUTE_TYPE, lang, prefix));

/**
* Iterate config and build document object:
* E.g.
* {
path: 'docs/en/other.md', <-- full path here
type: 'route', <--- Defined in file.js
lang: 'en' <-- Defined in file.js
}
*/
const buildFileList = async () => {
const config = require(`../.vuepress/config`);
let fileList = [];
Object.keys(config.themeConfig.locales).forEach((key) => {
const locale = config.themeConfig.locales[key];
const key_path = key.slice(1);
if (locale.hasOwnProperty('sidebar')) {
fileList = fileList.concat(loopType(locale.sidebar[key], locale.lang, key_path));
}
if (locale.hasOwnProperty('nav')) {
fileList = fileList.concat(loopNav(locale.nav, locale.lang));
}
});

return fileList;
};

/**
* Select files that only being modified
* Same format as `buildFileList()`
*/
const buildStagedList = async () => {
const stagedFiles = await sgf();
const stagedFileList = [];
stagedFiles.forEach((e) => {
if (e.filename.endsWith('.md')) {
stagedFileList.push(e.filename);
}
});
const fullFileList = await buildFileList();
const result = [];
stagedFileList.forEach((e) => {
const f = fullFileList.find((x) => x.path.indexOf(e) !== -1);
if (f) {
result.push(f);
}
});

return result;
};

/** Entry
* Usage: node format.js --full/--staged
*/
(async () => {
// Mode
const flag = process.argv[2] || '--full';
let fileList = [];
switch (flag) {
case '--staged':
fileList = await buildStagedList();
break;
case '--full':
default:
fileList = await buildFileList();
}
// console.log(fileList);
// return

for (const processor of processors) {
// We don't want to mix up processor
/* eslint-disable no-await-in-loop */
await Promise.all(
processor.rules(fileList).map(async (e) => {
let formatted = await file.readFile(e.path);
formatted = await processor.handler(formatted);
return file.writeFile(e.path, formatted);
})
).catch((err) => {
// eslint-disable-next-line no-console
console.log(err);
process.exit(1);
});
}
})();
57 changes: 57 additions & 0 deletions docs/.format/sortByHeading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const file = require('./file');
const pinyin = require('pinyin');

const isASCII = (str) => /^[\x00-\x7F]*$/.test(str);

module.exports = {
rules: (list) => list.filter((e) => e.type === file.ROUTE_TYPE),
handler: async (data) => {
const content = data.split('\n');
const blocks = [];
const h1 = [];

let i = 0;
while (i < content.length) {
const m = /^##\s*(.*)$/.exec(content[i]);
if (m) {
const b = {
title: m[1],
content: [],
};

b.content.push(content[i]);
i++;
while (i < content.length && !/^##\s.*$/.test(content[i])) {
b.content.push(content[i]);
i++;
}
blocks.push(b);
} else {
h1.push(content[i]);
i++;
}
}

let newContent = blocks
.sort((a, b) => {
const ia = isASCII(a.title[0]);
const ib = isASCII(b.title[0]);
if (ia && ib) {
return a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1;
} else if (ia || ib) {
return ia > ib ? -1 : 1;
} else {
return pinyin.compare(a.title, b.title);
}
})
.map((x) => x.content.join('\n'))
.join('\n');
if (newContent) {
h1.push(newContent);
}

newContent = h1.join('\n');

return Promise.resolve(newContent);
},
};
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RSSHub 是一个开源、简单易用、易于扩展的 RSS 生成器,可以

可以配合浏览器扩展 [RSSHub Radar](https://github.com/DIYgod/RSSHub-Radar) 食用

[Telegram 群](https://t.me/rsshub) | [Telegram 频道](https://t.me/awesomeRSSHub)
[Telegram 群](https://t.me/rsshub) \| [Telegram 频道](https://t.me/awesomeRSSHub)

NeverBehave marked this conversation as resolved.
Show resolved Hide resolved
## 鸣谢

Expand Down
16 changes: 8 additions & 8 deletions docs/anime.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pageClass: routes
::: tip 提示

番剧 id 不包含开头的 aa。
例如:http://www.acfun.cn/bangumi/aa5022158 的番剧 id 是 5022158,不包括开头的 aa。
例如:<http://www.acfun.cn/bangumi/aa5022158> 的番剧 id 是 5022158,不包括开头的 aa。

:::

Expand All @@ -45,7 +45,7 @@ pageClass: routes

<Route author="maple3142" example="/anime1/anime/2018年秋季/哥布林殺手" path="/anime1/anime/:time/:name" :paramsDesc="['时间', '动画名称']" radar="1">

时间和动画名称请自己从网址取得: <https://anime1.me/category/2018年秋季/刀劍神域-alicization>
时间和动画名称请自己从网址取得: [https://anime1.me/category/2018 年秋季 / 刀劍神域 - alicization](https://anime1.me/category/2018年秋季/刀劍神域-alicization)
NeverBehave marked this conversation as resolved.
Show resolved Hide resolved

</Route>

Expand Down Expand Up @@ -115,9 +115,9 @@ pageClass: routes

<Route author="cssxsh" example="/dlsite/new/home" path="/dlsite/new/:type" :paramsDesc="['类型,如下表']">

| 同人 | 漫画 | 软件 | 同人(R18) | 漫画(R18) | 美少女游戏 | 乙女 | BL |
| ---- | ----- | ---- | --------- | --------- | ---------- | ----- | --- |
| home | comic | soft | maniax | books | pro | girls | bl |
| 同人 | 漫画 | 软件 | 同人 (R18) | 漫画 (R18) | 美少女游戏 | 乙女 | BL |
| ---- | ----- | ---- | ---------- | ---------- | ---------- | ----- | --- |
| home | comic | soft | maniax | books | pro | girls | bl |

</Route>

Expand Down Expand Up @@ -196,7 +196,7 @@ pageClass: routes

<Route author="machsix" path="/webtoons/:lang/:category/:name/:id" example="/webtoons/zh-hant/drama/gongzhuweimian/894" :paramsDesc="['语言','类别','名称','ID']"/>

比如漫画公主彻夜未眠的网址为https://www.webtoons.com/zh-hant/drama/gongzhuweimian/list?title_no=894, 则`lang=zh-hant`,`category=drama`,`name=gongzhucheyeweimian`,`id=894`.
比如漫画公主彻夜未眠的网址为<https://www.webtoons.com/zh-hant/drama/gongzhuweimian/list?title_no=894>, 则`lang=zh-hant`,`category=drama`,`name=gongzhucheyeweimian`,`id=894`.

### [Naver](https://comic.naver.com)

Expand All @@ -208,8 +208,8 @@ pageClass: routes

<Route author="SunShinenny" path="/dilidili/fanju/:id" example="/dilidili/fanju/onepunchman2" :paramsDesc="['番剧id']">

请打开对应番剧的纵览页(非具体某集),从 url 中最后一位查看番剧 id.(一般为英文)
除去'海贼'此类具有特殊页面的超长番剧,绝大多数页面都可以解析.
请打开对应番剧的纵览页 (非具体某集), 从 url 中最后一位查看番剧 id.(一般为英文)
除去 ' 海贼 ' 此类具有特殊页面的超长番剧绝大多数页面都可以解析.
最适合用来追新番

</Route>
Expand Down
8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# API 接口

::: warning 注意
API 仍处于开发状态中,  并可能会有改动. 欢迎提供建议!
API 仍处于开发状态中 并可能会有改动欢迎提供建议!
:::

RSSHub 提供下列 API 接口:
Expand All @@ -18,9 +18,9 @@ RSSHub 提供下列 API 接口:

参数:

- name, 路由一级名称, 对应 [https://github.com/DIYgod/RSSHub/tree/master/lib/routes](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中的文件夹名称. 可选, **缺省则返回所有可用路由**.
- name, 路由一级名称对应 <https://github.com/DIYgod/RSSHub/tree/master/lib/routes> 中的文件夹名称。可选,**缺省则返回所有可用路由**.

成功请求将会返回 HTTP 状态码 `200 OK` 与 JSON 结果, 格式如下:
成功请求将会返回 HTTP 状态码 `200 OK` 与 JSON 结果格式如下:

```js
{
Expand Down Expand Up @@ -58,4 +58,4 @@ RSSHub 提供下列 API 接口:
}
```

若无符合请求路由, 请求将会返回 HTTP 状态码 `204 No Content`.
若无符合请求路由请求将会返回 HTTP 状态码 `204 No Content`.
20 changes: 10 additions & 10 deletions docs/bbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ pageClass: routes

## Discuz

### 通用子版块-自动检测
### 通用子版块 - 自动检测

<Route author="junfengP" example="/discuz/http%3a%2f%2fwww.u-share.cn%2fforum.php%3fmod%3dforumdisplay%26fid%3d56" path="/discuz/:link" :paramsDesc="['子版块链接, 需要手动Url编码']"/>

### 通用子版块-指定版本
### 通用子版块 - 指定版本

<Route author="junfengP" example="/discuz/x/https%3a%2f%2fwww.52pojie.cn%2fforum-16-1.html" path="/discuz/:ver/:link" :paramsDesc="['discuz版本类型,见下表','子版块链接, 需要手动Url编码']" >

Expand All @@ -72,7 +72,7 @@ pageClass: routes

</Route>

### 通用子版块-支持 Cookie
### 通用子版块 - 支持 Cookie

<Route author="junfengP" example="/discuz/x/00/https%3a%2f%2fbbs.zdfx.net%2fforum-2-1.html" path="/discuz/:ver/:cid/:link" :paramsDesc="['discuz版本类型,见下表', 'Cookie id,需自建并配置环境变量,详情见部署页面的配置模块','子版块链接, 需要手动Url编码']" >

Expand Down Expand Up @@ -150,7 +150,7 @@ pageClass: routes

## V2EX

### 最热/最新主题
### 最热 / 最新主题

<Route author="WhiteWorld" example="/v2ex/topics/latest" path="/v2ex/topics/:type" :paramsDesc="['hot 或 latest']"/>

Expand Down Expand Up @@ -304,12 +304,12 @@ pageClass: routes
<Route author="NavePnow" example="/1point3acres/offer/12/null/CMU" path="/1point3acres/offer/:year?/:major?/:school?" :paramsDesc="['录取年份 id,空为null', '录取专业 id,空为null', '录取学校 id,空为null']">
::: warning 三个 id 获取方式

1. 打开 https://offer.1point3acres.com
2. 打开控制台
3. 切换到 Network 面板
4. 点击 搜索 按钮
5. 点击 results?ps=15&pg=1 POST 请求
6. 找到 Request Payload 请求参数,例如 filters: {planyr: "13", planmajor: "1", outname_w: "ACADIAU"} ,则三个 id 分别为: 13,1,ACADIAU
1. 打开 <https://offer.1point3acres.com>
2. 打开控制台
3. 切换到 Network 面板
4. 点击 搜索 按钮
5. 点击 results?ps=15&pg=1 POST 请求
6. 找到 Request Payload 请求参数,例如 filters: {planyr: "13", planmajor: "1", outname_w: "ACADIAU"} ,则三个 id 分别为: 13,1,ACADIAU

:::
</Route>
Expand Down
2 changes: 1 addition & 1 deletion docs/blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pageClass: routes
| ----------------- | ------------------- | -------- | -------- | --------------- | -------- | -------------- |
| hirasawayui | se7en | walnut | themez | comeet | sunskyxh | zmd |

> 原则上只要是{type}.hedwig.pub 都可以匹配。
> 原则上只要是 {type}.hedwig.pub 都可以匹配。

## Hexo

Expand Down
4 changes: 2 additions & 2 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pageClass: routes

<Route author="MisteryMonster" example="/axis-studios/work/full-service-cg-production" path="/axis-studios/:type/:tag?" :paramsDesc="['`work`, `blog`','文章内的 Work type URL: `compositing`, `full-service-cg-production`, `vfx-supervision`, `realtime`, `art-direction`, `animation`']">

文章内 Work type 指向的栏目地址,比如: 'https://axisstudiosgroup.com/work/full-service-cg-production' 的 tag 为 `full-service-cg-production`,要注意的是 tag 和文章的目录是一样的。
文章内 Work type 指向的栏目地址,比如: '<https://axisstudiosgroup.com/work/full-service-cg-production'> 的 tag 为 `full-service-cg-production`,要注意的是 tag 和文章的目录是一样的。
NeverBehave marked this conversation as resolved.
Show resolved Hide resolved

有一些 tag 并不经常使用: `Script`, `direction`, `production`, `design-concept` 等等。

Expand Down Expand Up @@ -63,7 +63,7 @@ pageClass: routes

不支持`news`和`main`。

默认为 'https://www.methodstudios.com/en/features' 下的栏目。
默认为 '<https://www.methodstudios.com/en/features'> 下的栏目。
NeverBehave marked this conversation as resolved.
Show resolved Hide resolved

</Route>

Expand Down
2 changes: 1 addition & 1 deletion docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

**Q: 演示地址可以用么?**

**A:** 演示地址为 [rsshub.app](https://rsshub.app), 缓存时间 20 分钟, 可以随意使用。但如果你看到路由有 <Badge text="反爬严格" vertical="middle" type="warn"/> 标记,如微博、知乎等,意味着目标网站有严重的反爬策略,demo 无法确保可用性,建议自建来提高稳定性。
**A:** 演示地址为 [rsshub.app](https://rsshub.app), 缓存时间 20 分钟可以随意使用。但如果你看到路由有 <Badge text="反爬严格" vertical="middle" type="warn"/> 标记,如微博、知乎等,意味着目标网站有严重的反爬策略,demo 无法确保可用性,建议自建来提高稳定性。

**Q: 为什么 RSSHub 里的图片加载不出来?**

Expand Down
Loading