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

docs: format docs using pangu #4654

Closed
wants to merge 1 commit into from
Closed

docs: format docs using pangu #4654

wants to merge 1 commit into from

Conversation

HenryQW
Copy link
Collaborator

@HenryQW HenryQW commented May 2, 2020

@vercel
Copy link

vercel bot commented May 2, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/diy/rsshub/6imrrahg5
✅ Preview: https://rsshub-git-fork-henryqw-docs.diy.now.sh

@vercel vercel bot temporarily deployed to Preview May 2, 2020 19:35 Inactive
@NeverBehave
Copy link
Collaborator

https://github.com/VincentBel/remark-pangu

考虑直接写进format?

@HenryQW
Copy link
Collaborator Author

HenryQW commented May 3, 2020

https://github.com/VincentBel/remark-pangu

考虑直接写进format?

const remark = require('remark');
const pangu = require('remark-pangu');
const fs = require('fs');
const pinyin = require('pinyin');
const path = require('path');
const isASCII = (str) => /^[\x00-\x7F]*$/.test(str);

const sortByHeading = async (filePath) => {
    fs.readFile(filePath, 'utf8', (err, data) => {
        if (err) {
            return Promise.reject(err);
        }
        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');

        remark()
            .use(pangu)
            .process(newContent, (err, result) => {
                if (err) {
                    return Promise.reject(err);
                }
                newContent = String(result);
            });

        fs.writeFile(filePath, newContent, 'utf8', (err) => {
            if (err) {
                return Promise.reject(err);
            } else {
                return Promise.resolve(newContent);
            }
        });
    });
};

(async () => {
    const config = require(`./.vuepress/config`);

    let fileList = Object.keys(config.themeConfig.locales)
        .map((key) => {
            const locale = config.themeConfig.locales[key];
            if (locale.hasOwnProperty('sidebar')) {
                if (locale.sidebar['/']) {
                    return locale.sidebar['/'][1].children.map((x) => path.resolve(__dirname, `./${x}.md`));
                } else if (locale.sidebar['/en/']) {
                    return locale.sidebar['/en/'][1].children.map((x) => path.resolve(__dirname, `./en/${x}.md`));
                }
                return null;
            } else {
                return null;
            }
        })
        .filter((x) => !!x);

    fileList = [].concat.apply([], fileList);

    fileList.forEach((filePath) => {
        sortByHeading(filePath)
            .then(() => {
                // console.log(`Processed ${filePath}`);
            })
            .catch((err) => {
                throw err;
            });
    });
})();

试了不行,会把头部的

---
pageClass: routes
---

弄得很奇怪

image

@NeverBehave
Copy link
Collaborator

确实, 似乎对于表格的优化也有点问题, 怎么感觉是remark的锅

Diff: https://pastebin.com/Wq2rGFP9

之前还打算在博客用上这个自动整理

@NeverBehave
Copy link
Collaborator

https://github.com/remarkjs/remark-frontmatter 哦我大概知道怎么回事了

@NeverBehave
Copy link
Collaborator

NeverBehave commented May 3, 2020

https://github.com/NeverBehave/RSSHub/commits/pangu

我只能说这是个大坑, 目前效果是还行但是小问题有些解决不了

  • 文档里面md风格有不少差距, 我建议可以的话以后以GFM为标准

目前中文自动修正的效果: NeverBehave@02b1c9f

部分链接位置, 段落间距由于不规范会被错误的切割, 这个可以通过复查一下解决
我在这里已经修正一些格式不规范的问题

  • Prettier会和remark部分操作冲突
    这个行为我已经尝试统一了但是还是有两个问题我解决不了:

  • 表格最小长度为3

  • 在计算字符居中位置的时候, remark是向上取整, prettier是向下取整

差异比较: NeverBehave@a7a85c0

这个行为不规范每次会污染一些未添加进staged的文件, 目前我没有好的解决方案 @DIYgod

折腾了一天以后宣告放弃真的不甘心
我还是决定至少搞完mmp

@HenryQW
Copy link
Collaborator Author

HenryQW commented May 3, 2020

不光是表格,还有链接

image

@NeverBehave
Copy link
Collaborator

#4656

这个可以 close 了?

@DIYgod DIYgod closed this May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants