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

use promise.all to maximize parallelism #53

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
18 changes: 10 additions & 8 deletions oi-wiki-export-typst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import remarkMath from 'remark-math'
import remarkDetails from 'remark-details'
import remarkGfm from 'remark-gfm'
import remarkTabbed from 'remark-tabbed'
import { read, writeSync } from 'to-vfile'
import { read, write } from 'to-vfile'
import { Type, Schema, load } from 'js-yaml'

import { snippet as _snippet } from './snippet.js'
Expand Down Expand Up @@ -81,11 +81,13 @@ async function main() {
const catalog = config.nav // 文档目录

let includes = ''
const exportPromises = []
for (const id in catalog) {
const texModule = join('typ', id.toString())
await fs.writeFile(texModule + '.typ', await exportRecursive(catalog[id], 0))
await fs.writeFile(texModule + '.typ', exportRecursive(catalog[id], 0))
includes += '#include "' + texModule + '.typ"\n' // 输出 includes.typ 章节目录文件
}
await Promise.all(exportPromises)

await fs.writeFile('includes.typ', includes)
console.log(INFO + 'Export successful.')
Expand All @@ -107,7 +109,6 @@ async function main() {
.use(remarkGfm)
.use(remarkDetails)
.use(remarkTabbed)
// .use(remarkTabbed)
.use(remarkTypst, {
prefix: filename.replace(PREFIX_REGEX, '').replace(/md$/, ''), // 根据路径生成 ID,用作 label
depth: depth, // 标题 h1 深度
Expand All @@ -118,27 +119,28 @@ async function main() {
path: filename.replace(/\.md$/, '/'), // 由文件名转换而来的路径
title: title,
})
.process(await read(filename), (err, file) => {
.process(await read(filename), async (err, file) => {
if (err) {
throw err
}
file.dirname = 'typ'
file.stem = filename.replace(PREFIX_REGEX, '')
file.extname = '.typ'
writeSync(file)
await write(file)
})
}

// 递归处理各个 chapter
async function exportRecursive(object, depth) {
function exportRecursive(object, depth) {
let result = ''
depth = Math.min(depth, 6)

for (const key in object) {
console.log(INFO + 'Exporting: ' + key)

if (typeof object[key] === 'string') { // 对应页面
await convertMarkdown(join(oiwikiRoot, 'docs', object[key]), depth + 1, object[key])
const convert_promise = convertMarkdown(join(oiwikiRoot, 'docs', object[key]), depth + 1, object[key])
exportPromises.push(convert_promise)

const moduleName = escape(getModuleName(object[key]))
result += '{0} {1} <{2}>\n'.format(
Expand All @@ -158,7 +160,7 @@ async function main() {
}

for (const id in object[key]) {
result += await exportRecursive(object[key][id], depth + 1)
result += exportRecursive(object[key][id], depth + 1)
}
}
}
Expand Down
Loading