-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pangu' of git://github.com/NeverBehave/RSSHub into Neve…
…rBehave-pangu
- Loading branch information
Showing
35 changed files
with
904 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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, | ||
link: false, | ||
}) | ||
.use(prettier) | ||
.use({ | ||
settings: { | ||
stringLength: width, | ||
}, | ||
}) | ||
.process(doc); | ||
return typeof result === 'string' ? result : typeof result.contents === 'string' ? result.contents : result.result; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
1dc53aa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: