-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-catalog.js
29 lines (17 loc) · 997 Bytes
/
make-catalog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { readFileSync, writeFileSync } from 'fs';
import { parse, resolve } from 'path';
/** @param {string} fileMarkdown */
const make = fileMarkdown => {
const linesRaw = readFileSync(fileMarkdown, 'utf-8').split('\n');
const linesCatalog = linesRaw
.filter(line => line.startsWith('### '))
.map(line => line.replace(/(^### |\*\*)/g, ''))
.map(title => `* [${title}](#${title.replace(/[、/:|[\]<>]/g, '').replace(/ /g, '-')})`);
const indexCatalog = linesRaw.indexOf('## Catalog:目录');
const indexLineHeader = linesRaw.indexOf('## Lines:台词');
const linesResult = linesRaw.filter((line, index) => index <= indexCatalog || index >= indexLineHeader);
linesResult.splice(indexCatalog + 1, 0, ...linesCatalog);
writeFileSync(resolve(fileMarkdown, '..', `${parse(fileMarkdown).name}.md`), linesResult.join('\n'));
writeFileSync(resolve(fileMarkdown, '..', `${parse(fileMarkdown).name}.bak.md`), linesRaw.join('\n'));
};
if(process.argv[2]) { make(process.argv[2]); }