-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3943881
commit d5d6202
Showing
5 changed files
with
127 additions
and
103 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
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 |
---|---|---|
@@ -1,10 +1,37 @@ | ||
#!/usr/bin/env bun | ||
|
||
import { main } from '../lib/main' | ||
|
||
// 获取命令行参数 | ||
import { Options, renderMarkdown } from '../lib/main.ts' | ||
/** 命令行参数 */ | ||
const args = process.argv.slice(2) | ||
/** 当前工作目录 */ | ||
const cwd = process.cwd() | ||
|
||
// 渲染 markdown | ||
main(args, cwd) | ||
/** | ||
* 主函数 | ||
* @param args 命令行参数 | ||
* @param cwd 当前工作目录 | ||
*/ | ||
void async function main(args: string[], cwd: string) { | ||
try { | ||
// 如果没有参数, 显示帮助信息 | ||
if (args.length === 0) { | ||
console.log(`\n使用方法:\n${Options.format}\n`) | ||
process.exit(0) | ||
} | ||
// 解析参数 | ||
console.log('\n开始生成\n') | ||
const options = new Options(args, cwd) | ||
// 渲染 markdown | ||
await renderMarkdown(options) | ||
console.log('生成成功\n') | ||
} | ||
catch (e) { | ||
if (e instanceof SyntaxError) { | ||
console.error(`参数错误, 正确格式:\n${Options.format}\n`) | ||
} else if (e instanceof Error) { | ||
console.error(`错误:\n${e.message}\n`) | ||
} | ||
} | ||
finally { | ||
process.exit(0) | ||
} | ||
}(args, cwd) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** 将英文标点转换为中文标点 */ | ||
export function en2zh() { | ||
const nodes = document.querySelectorAll('body > p, .abstract, .keywords, .author, .school') | ||
nodes.forEach(node => { | ||
let text = node.textContent ?? '' | ||
// 替换中英文逗号和句号 | ||
text = text.replace(/, /g, ',').replace(/\. /g, '。').replace(/\.$/, '。') | ||
// 替换中英文冒号 | ||
text = text.replace(/: /g, ':') | ||
// 替换中英文分号 | ||
text = text.replace(/; /g, ';') | ||
// 替换中英文感叹号 | ||
text = text.replace(/! /g, '!').replace(/!$/, '!') | ||
// 替换中英文问号 | ||
text = text.replace(/\? /g, '?').replace(/\?$/, '?') | ||
// 替换中英文括号 | ||
text = text.replace(/ \(/g, '(').replace(/\) /g, ')') | ||
// 恢复et al., | ||
text = text.replace(/et al\.,/g, 'et al., ').replace(/et al。/g, 'et al. ') | ||
// 设置新文本 | ||
node.textContent = text | ||
}) | ||
} | ||
|
||
/** 将中文标点转换为英文标点 */ | ||
export function zh2en() { | ||
const nodes = document.querySelectorAll('body > p, .abstract, .keywords, .author, .school') | ||
nodes.forEach(node => { | ||
let text = node.textContent ?? '' | ||
// 替换中英文逗号和句号 | ||
text = text.replace(/,/g, ', ').replace(/。 /g, '. ') | ||
// 替换中英文冒号 | ||
text = text.replace(/:/g, ': ') | ||
// 替换中英文分号 | ||
text = text.replace(/;/g, '; ') | ||
// 替换中英文感叹号 | ||
text = text.replace(/!/g, '! ') | ||
// 替换中英文问号 | ||
text = text.replace(/?/g, '? ') | ||
// 替换中英文括号 | ||
text = text.replace(/(/g, ' (').replace(/)/g, ') ') | ||
// 设置新文本 | ||
node.textContent = text | ||
}) | ||
} | ||
|
||
/** | ||
* 解析自定义标签 | ||
* @param md markdown 字符串 | ||
* @returns 转换后的 markdown 字符串 | ||
*/ | ||
export function parseCustomTags(md: string): string { | ||
// 作者 | ||
md = md.replace(/#author# (.*)/mg, '<div class="author">$1</div>') | ||
// 单位 | ||
md = md.replace(/#school# (.*)/mg, '<div class="school">$1</div>') | ||
// 关键词 | ||
md = md.replace(/#keywords# (.*)/mg, '<div class="keywords">$1</div>') | ||
// 摘要 | ||
md = md.replace(/#abstract# (.*)/mg, '<div class="abstract">$1</div>') | ||
// 返回处理后的字符串 | ||
return md | ||
} |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "leaf-markdown-paper", | ||
"type": "module", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"author": { | ||
"name": "LeafYeeXYZ", | ||
"email": "[email protected]" | ||
|