Skip to content

Commit

Permalink
perf: optimize scripts (apache#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeYoung authored Aug 5, 2022
1 parent 2994fcd commit 92e17e3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 332 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/link-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Pull Docs
run: |
yarn sync-doc && git status
yarn sync-docs && git status
- name: Check Links
run: |
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
"scripts": {
"sync-doc": "yarn workspace scripts sync",
"sync-docs": "yarn workspace scripts sync-docs",
"generate-repos-info": "yarn workspace scripts generate-repos-info",
"generate-picked-posts": "yarn workspace scripts generate-picked-posts",
"update-sitemap": "yarn workspace scripts update-sitemap",
Expand All @@ -35,7 +35,7 @@
"serve:blog:zh": "yarn workspace blog docusaurus serve zh",
"serve:blog:en": "yarn workspace blog docusaurus serve en",
"prepare": "husky install",
"prepare-data": "yarn sync-doc && yarn generate-repos-info && yarn generate-picked-posts"
"prepare-data": "yarn sync-docs && yarn generate-repos-info && yarn generate-picked-posts"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.17.0",
Expand Down
51 changes: 10 additions & 41 deletions scripts/generate-picked-posts-info.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,19 @@ import { join, dirname } from 'path';
import Listr from 'listr';
import matter from 'gray-matter';
import { remark } from 'remark';
import mdx from 'remark-mdx';
import remarkComment from 'remark-comment';
import { visit } from 'unist-util-visit';
import { format } from 'date-fns';

const configs = ['../blog/en/config/picked-posts.json', '../blog/zh/config/picked-posts.json'];

const parser = remark().use(mdx).use(remarkComment);
const isImage = (node) => node.type === 'image';
const isHeading = (node) => node.type === 'heading';
const isParagraph = (node) => node.type === 'paragraph';
const isText = (node) => node.type === 'text';

function toText(node) {
let excerpt = '';
visit(node, ['text', 'inlineCode'], (child, index, parent) => {
if (parent?.type !== 'linkReference') {
excerpt += child.value;
}
});
return excerpt;
}
const parser = remark();

function createExcerpt(fileString) {
const mdast = parser.parse(fileString);
let excerpt = '';
visit(mdast, ['paragraph', 'heading', 'image'], (node) => {
const isAdmonitionFence =
isParagraph(node) && isText(node.children[0]) && node.children[0].value.startsWith(':::');
const isMainHeading = isHeading(node) && node.depth === 1;
if (isAdmonitionFence || isMainHeading) {
return true;
}
if (isImage(node)) {
if (node.alt) {
excerpt = node.alt;
return false;
}
} else if (isParagraph(node)) {
excerpt = toText(node);
}
if (excerpt) {
return false;
}
return true;
visit(mdast, ['text', 'inlineCode'], (node) => {
excerpt += node.value;
});

return excerpt || undefined;
return excerpt;
}

const tasks = new Listr([
Expand Down Expand Up @@ -78,7 +43,11 @@ const tasks = new Listr([
Promise.all(
paths.map((path) =>
readFile(`../${path}`, 'utf8').then((content) => {
const { data, content: c } = matter(content);
const { data, excerpt } = matter(content, {
excerpt: true,
excerpt_separator: '<!--truncate-->',
});
const summary = createExcerpt(excerpt);
const locale = path.includes('/zh/blog') ? 'zh-CN' : 'en-US';
const rawDate = new Date(
path.substring('blog/en/blog/'.length, 'blog/en/blog/2022/07/30'.length)
Expand All @@ -103,7 +72,7 @@ const tasks = new Listr([
permalink:
locale === 'zh-CN' ? '/zh/blog/tags/' + v : '/blog/tags/' + v,
})) || [],
summary: createExcerpt(c),
summary,
permalink: path
.substring(locale === 'zh-CN' ? 'blog'.length : 'blog/en'.length)
.slice(0, -'.md'.length),
Expand Down
8 changes: 3 additions & 5 deletions scripts/generate-repos-info.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs/promises');
const { writeFile } = require('fs/promises');
const axios = require('axios');
const Listr = require('listr');

Expand All @@ -8,9 +8,7 @@ const axiosConfig = {
headers: {
'content-type': 'application/json',
Accept: 'application/vnd.github.v3+json',
...(process.env.GITHUB_TOKEN
? { Authorization: `token ${process.env.GITHUB_TOKEN}` }
: {}),
...(process.env.GITHUB_TOKEN ? { Authorization: `token ${process.env.GITHUB_TOKEN}` } : {}),
},
};

Expand Down Expand Up @@ -62,7 +60,7 @@ const tasks = new Listr([
},
{
title: `Save repos' info and good first issues to json file`,
task: () => fs.writeFile('../config/repos-info.json', JSON.stringify(res)),
task: () => writeFile('../config/repos-info.json', JSON.stringify(res)),
},
]);

Expand Down
4 changes: 1 addition & 3 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "scripts",
"version": "1.0.0",
"scripts": {
"sync": "node sync-docs.js",
"sync-docs": "node sync-docs.js",
"link-checker": "node link-checker.js",
"generate-repos-info": "node generate-repos-info.js",
"update-sitemap": "node update-sitemap-loc.js",
Expand All @@ -21,8 +21,6 @@
"dependencies": {
"date-fns": "^2.29.1",
"remark": "^14.0.2",
"remark-comment": "^1.0.0",
"remark-mdx": "^2.1.2",
"unist-util-visit": "^4.1.0"
}
}
Loading

0 comments on commit 92e17e3

Please sign in to comment.