This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
my-extension.ts
82 lines (70 loc) · 3.45 KB
/
my-extension.ts
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
// const {Translation, extractOrApplyTranslations} = require("./scripts/utils");
const Asciidoctor = require('@asciidoctor/core')();
// import asciidoctor, { Asciidoctor } from "asciidoctor";
import { extractOrApplyTranslations, macroPageBreak, Translation } from "./scripts/utils";
// const asciidoc = asciidoctor();
module.exports.register = function ({ config }) {
const translation = new Translation();
const registry = Asciidoctor.Extensions.create();
registry.treeProcessor(function () {
extractOrApplyTranslations.call(this, translation);
});
const asciiLoad = Asciidoctor.load;
Asciidoctor.load = function (...args) {
const options = args[1];
if (!options.extension_registry.extRegistered) {
options.extension_registry.treeProcessor(function () {
extractOrApplyTranslations.call(this, translation);
});
options.extension_registry.extRegistered = true;
}
translation.setLanguage(args[1].attributes['page-component-name']);
const document = asciiLoad.call(this, ...args);
// console.log('YOLO', document.reader?.file, args[1].attributes['page-component-name']);
// console.log('YOLO', document);
return document;
}
this.on('contentClassified', async (event) => {
const { playbook, contentCatalog } = this.getVariables();
const options = {
extension_registry: registry,
safe: 'server',
base_dir: 'src/modules/ROOT/pages'
};
console.log('contentCatalog', contentCatalog.getComponents()[0]);
// console.log('lang', this.getVariables());
// console.log('contentCatalog', contentCatalog.getComponents());
// translation.setLanguage(args[1].attributes.lang);
const components: { [name: string]: true } = {};
for (const page of contentCatalog.getPages()) {
components[page.src.component] = true;
}
for (const component of Object.keys(components)) {
// console.log('Load component', component);
translation.setLanguage(component);
for (const page of contentCatalog.getPages()) {
if (page.src.component !== component) continue;
const content = page.contents.toString('utf8');
//trigger Translation extension to extract language
asciiLoad.call(Asciidoctor, content, options);
}
console.log('Load languages for', component);
await translation.loadTranslations();
}
});
this.on('documentsConverted', (event) => {
const { playbook, contentCatalog } = this.getVariables();
for (const page of contentCatalog.getPages()) {
translation.setLanguage(page.asciidoc.attributes.lang);
page.asciidoc.doctitle = translation.get(page.asciidoc.doctitle);
page.asciidoc.navtitle = translation.get(page.asciidoc.navtitle);
}
});
// this.on('beforePublish', (event) => {
// const { playbook, contentCatalog, siteCatalog } = this.getVariables()
// console.log('siteCatalog', contentCatalog.getPages()[0].contents.toString('utf8'));
// // console.log('contentCatalog', contentCatalog.getComponents()[0]);
// // console.log('pagesComposed', this.getVariables());
// // console.log('pagesComposed', contentCatalog[Object.getOwnPropertySymbols(contentCatalog)[0]].get('default'));
// })
}