-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciidoctor.js
91 lines (78 loc) · 2.21 KB
/
asciidoctor.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const Asciidoctor = require('asciidoctor');
const kroki = require('asciidoctor-kroki');
const highlightJsExt = require('asciidoctor-highlight.js');
const glob = require('glob');
const fs = require('fs');
const cheerio = require('cheerio');
const asciidoctor = Asciidoctor();
kroki.register(asciidoctor.Extensions);
highlightJsExt.register(asciidoctor.Extensions);
const options = {
safe: 'unsafe',
standalone: true,
attributes: {
linkcss: true,
nofooter: true,
sectanchors: true,
'source-highlighter': 'highlightjs-ext',
docinfodir: '../res/',
docinfo: 'shared',
stylesheet: '../res/asciidoc.css',
'kroki-default-options': 'inline',
'allow-uri-read': true,
},
};
if (process.argv[2]) {
options.to_file = false;
const html = asciidoctor.convertFile(process.argv[2], options);
console.log(addTargetBlankToExternalLinks(html));
} else {
const path = require('path');
// Change the current working directory to a new path
const newPath = path.join(__dirname, 'blog');
process.chdir(newPath);
// Get the new current working directory
console.log(process.cwd());
glob('*.adoc', (err, files) => {
if (err) {
console.log(err);
return;
}
files.forEach((file) => {
fs.readFile(file, 'utf8', (err, asciiDocSource) => {
if (err) {
console.log(err);
return;
}
const html = asciidoctor
.convert(asciiDocSource.replace(/ /g, 'ツ').replace(/[\u2028]/g, '<br>'), options)
.replace(/ツ/g, ' ');
fs.writeFile(
file.replace(/adoc$/, 'html'),
addTargetBlankToExternalLinks(html),
(err) => {
if (err) {
console.log(err);
return;
}
}
);
});
});
});
}
function addTargetBlankToExternalLinks(html) {
const $ = cheerio.load(html);
// Make external links open in a new tab
$('a').each(function () {
const href = $(this).attr('href');
if (href) {
if (/^https?:\/\//i.test(href)) {
$(this).attr('target', '_blank');
}
}
});
// Do not include stylesheets in the documents. Style are inherited from the main page.
$('link').remove();
return $.html();
}