Skip to content

Commit

Permalink
feat(script): generate ToC from .meta/toc.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ourai committed Apr 9, 2024
1 parent cf8338d commit c3b4aa5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mirror-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
dst_token: ${{ secrets.GITEE_TOKEN }}
account_type: org
static_list: "site"
static_list: site
force_update: true
debug: true
87 changes: 77 additions & 10 deletions scripts/gen/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,117 @@
const { resolve: resolvePath } = require('path');
const { isPlainObject, capitalize } = require('@ntks/toolbox');
const { existsSync } = require('fs');
const { isArray, isPlainObject, capitalize } = require('@ntks/toolbox');

const { getConfig, ensureDirExists, readData, saveData, execute, resolveSiteSrcDir } = require('../helper');
const { resolveRootPath, getConfig, ensureDirExists, readData, saveData, normalizeFrontMatter, execute, resolveSiteSrcDir } = require('../helper');

function resolveDocToc(docs, docData) {
function resolveSlug(uri) {
return `${uri.replace(/(?:\/)?(index)?\.md$/, '') || 'index'}`;
}

function resolveDefaultDocToc(docs, docData) {
return docs.map(({ title, uri, children }) => {
const resolved = {};

if (title) {
resolved.text = title;
}

const slug = `${uri.replace(/(?:\/)?(index)?\.md$/, '') || 'index'}`;
const slug = resolveSlug(uri);

resolved.slug = slug;
docData[slug] = { title: title || '', slug };

if (children) {
resolved.items = resolveDocToc(children, docData);
resolved.items = resolveDefaultDocToc(children, docData);
}

return resolved;
});
}

function resolveCustomizedDocToc(srcPath, items, parentUri, docData) {
const resolved = [];

items.forEach(({ text, uri, children }) => {
const item = {};

if (text) {
item.text = text;
}

if (isArray(children)) {
item.items = resolveCustomizedDocToc(srcPath, children, uri, docData);
} else {
if (uri) {
let docFile;

if (uri.startsWith('./')) {
docFile = uri;
} else {
docFile = `./${parentUri ? [parentUri.replace(/^\\\./, ''), uri].join('/') : uri}`;
}

const docPath = resolvePath(srcPath, docFile);

if (!existsSync(docPath)) {
return;
}

if (!item.text) {
const content = readData(docPath);

if (content) {
const normalized = normalizeFrontMatter(content);

if (normalized.data && normalized.data.title) {
item.text = normalized.data.title;
}
}
}

item.slug = resolveSlug(docFile.slice(2));
docData[item.slug] = { title: item.text || '', slug: item.slug };
}
}

resolved.push(item);
});

return resolved;
}

function resolveDocToc(srcPath, docs, docData) {
const customizedTocPath = `${srcPath}/.meta/toc.yml`;

if (existsSync(customizedTocPath)) {
return resolveCustomizedDocToc(srcPath, readData(customizedTocPath), '', docData);
}

return resolveDefaultDocToc(docs.structure, docData);
}

function resolveRepoData(config, site) {
const rootPath = resolveRootPath();
const cookbook = readData(resolvePath(__dirname, './cookbook.yml'));
const siteDataDir = resolvePath(__dirname, '../..', `${resolveSiteSrcDir(site)}${config.generator === 'hexo' ? '/source' : ''}/_data`);
const siteDataDir = resolvePath(rootPath, `${resolveSiteSrcDir(site)}${config.generator === 'hexo' ? '/source' : ''}/_data`);

const projectRepos = {};

Object.keys(config.data).forEach(srcKey => {
Object.entries(config.data).forEach(([srcKey, srcDir]) => {
if (!srcKey.startsWith('project-')) {
return;
}

const docs = readData(`${siteDataDir}/knosys/${srcKey}/docs.yml`);
const docData = {};
const toc = resolveDocToc(resolvePath(rootPath, srcDir), readData(`${siteDataDir}/knosys/${srcKey}/docs.yml`), docData);

const projectSlug = srcKey.replace(/^project\-/, '');

projectRepos[projectSlug] = {
name: `${projectSlug.split('-').map(w => capitalize(w)).join(' ')} 项目文档`,
base: `/projects/${projectSlug}`,
collection: 'docs',
toc: resolveDocToc(docs.structure, docData),
toc,
};

saveData(`${siteDataDir}/knosys/${projectSlug}.yml`, { items: docData });
Expand Down Expand Up @@ -75,7 +142,7 @@ module.exports = {

keys.forEach(key => execute('generate', site, key));

if (site === 'default') {
if (siteConfig.generator === 'hexo') {
setTimeout(() => resolveRepoData(siteConfig, site));
}
},
Expand Down
5 changes: 2 additions & 3 deletions scripts/serve.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { resolve: resolvePath } = require('path');
const { pick } = require('@ntks/toolbox');

const { getConfig, readData, saveData, execute, resolveSiteSrcDir } = require('./helper');

const rootPath = resolvePath(__dirname, '..');
const { resolveRootPath, getConfig, readData, saveData, execute, resolveSiteSrcDir } = require('./helper');

module.exports = {
execute: (site = 'default') => {
if (getConfig(`site.${site}.generator`) === 'hexo') {
const rootPath = resolveRootPath();
const pkg = readData(`${rootPath}/package.json`);
const siteSrcPath = resolvePath(rootPath, resolveSiteSrcDir(site));

Expand Down

0 comments on commit c3b4aa5

Please sign in to comment.