@antora/assembler 1.0.0-alpha.9 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,145 +0,0 @@
1
- 'use strict'
2
-
3
- const File = require('vinyl')
4
- const filterComponentVersions = require('./filter-component-versions')
5
- const produceAggregateDocument = require('./produce-aggregate-document')
6
- const selectMutableAttributes = require('./select-mutable-attributes')
7
-
8
- const IMAGE_MACRO_RX = /^image::?(.+?)\[(.*?)\]$/
9
-
10
- function produceAggregateDocuments (loadAsciiDoc, contentCatalog, assemblerConfig) {
11
- const { insertStartPage, rootLevel, sectionMergeStrategy, asciidoc: assemblerAsciiDocConfig } = assemblerConfig
12
- const assemblerAsciiDocAttributes = Object.assign({}, assemblerAsciiDocConfig.attributes)
13
- const { doctype, revdate, 'source-highlighter': sourceHighlighter } = assemblerAsciiDocAttributes
14
- delete assemblerAsciiDocAttributes.doctype
15
- delete assemblerAsciiDocAttributes.revdate
16
- delete assemblerAsciiDocAttributes['source-highlighter']
17
- return filterComponentVersions(contentCatalog.getComponents(), assemblerConfig.componentVersions).reduce(
18
- (accum, componentVersion) => {
19
- const { name: componentName, version, title, navigation } = componentVersion
20
- if (!navigation) return accum
21
- const componentVersionAsciiDocConfig = getAsciiDocConfigWithAsciidoctorReducerExtension(componentVersion)
22
- const mergedAsciiDocConfig = Object.assign({}, componentVersionAsciiDocConfig, {
23
- attributes: Object.assign({ revdate }, componentVersionAsciiDocConfig.attributes, assemblerAsciiDocAttributes),
24
- })
25
- const mergedAsciiDocAttributes = mergedAsciiDocConfig.attributes
26
- Object.entries(mergedAsciiDocAttributes).forEach(([name, val]) => {
27
- const match = name.endsWith('-image') && val.startsWith('image:') && IMAGE_MACRO_RX.exec(val)
28
- if (!(match && isResourceRef(match[1]))) return
29
- // Q should we allow image to be resolved relative to component version?
30
- const image = contentCatalog.resolveResource(match[1], undefined, 'image', ['image'])
31
- if (!image?.out) return
32
- mergedAsciiDocAttributes[name] = `image:${image.out.path}[${match[2]}]`
33
- image.out.assembled = true
34
- })
35
- const rootEntry = { content: title }
36
- let startPage = contentCatalog.getComponentVersionStartPage(componentName, version)
37
- if (startPage && startPage.src.component === componentName && startPage.src.version === version) {
38
- if (insertStartPage && !includedInNav(navigation, startPage.pub.url)) {
39
- Object.assign(rootEntry, { url: startPage.pub.url, urlType: 'internal' })
40
- }
41
- } else {
42
- // Q: should we always use a reference page as startPage for computing mutableAttributes?
43
- startPage = createFile({
44
- component: componentVersion.name,
45
- version: componentVersion.version,
46
- relative: '.reference-page.adoc',
47
- origin: (componentVersion.origins || [])[0],
48
- })
49
- }
50
- const mutableAttributes = selectMutableAttributes(loadAsciiDoc, contentCatalog, startPage, mergedAsciiDocConfig)
51
- delete mutableAttributes.doctype
52
- accum = accum.concat(
53
- prepareOutlines(navigation, rootEntry, rootLevel).map((outline) =>
54
- produceAggregateDocument(
55
- loadAsciiDoc,
56
- contentCatalog,
57
- componentVersion,
58
- outline,
59
- doctype,
60
- contentCatalog.getPages((page) => page.out),
61
- mergedAsciiDocConfig,
62
- mutableAttributes,
63
- sectionMergeStrategy
64
- )
65
- )
66
- )
67
- mergedAsciiDocAttributes.doctype = doctype
68
- sourceHighlighter
69
- ? (mergedAsciiDocAttributes['source-highlighter'] = sourceHighlighter)
70
- : delete mergedAsciiDocAttributes['source-highlighter']
71
- return accum
72
- },
73
- []
74
- )
75
- }
76
-
77
- function createFile (src) {
78
- const familySegment = (src.family ??= 'page') + 's'
79
- const path = `modules/${(src.module ??= 'ROOT')}/${familySegment}/${src.relative}`
80
- const moduleRootPath = Array(src.relative.split('/').length - 1)
81
- .fill('..')
82
- .join('/')
83
- const outPath = [
84
- src.component === 'ROOT' ? '' : src.component,
85
- src.version,
86
- src.module === 'ROOT' ? '' : src.module,
87
- src.family === 'page' ? '' : '_' + familySegment,
88
- src.family === 'page' ? src.relative.replace(/\.adoc$/, '.html') : src.relative,
89
- ]
90
- .filter((it) => it)
91
- .join('/')
92
- return new File({
93
- path,
94
- contents: src.contents ?? Buffer.alloc(0),
95
- src,
96
- out: { path: outPath },
97
- pub: { url: '/' + outPath, moduleRootPath },
98
- })
99
- }
100
-
101
- function getAsciiDocConfigWithAsciidoctorReducerExtension (componentVersion) {
102
- const asciidoctorReducerExtension = require('./asciidoctor/reducer-extension') // NOTE: must be required lazily
103
- const asciidocConfig = componentVersion.asciidoc
104
- const extensions = asciidocConfig.extensions || []
105
- if (extensions.length) {
106
- return Object.assign({}, asciidocConfig, {
107
- extensions: extensions.reduce(
108
- (accum, candidate) => {
109
- if (candidate !== asciidoctorReducerExtension) accum.push(candidate)
110
- return accum
111
- },
112
- [asciidoctorReducerExtension]
113
- ),
114
- sourcemap: true,
115
- })
116
- }
117
- return Object.assign({}, asciidocConfig, { extensions: [asciidoctorReducerExtension], sourcemap: true })
118
- }
119
-
120
- function includedInNav (items, url) {
121
- return items.find((it) => it.url === url || includedInNav(it.items || [], url))
122
- }
123
-
124
- function isResourceRef (target) {
125
- return ~target.indexOf(':') && !(~target.indexOf('://') || (target.startsWith('data:') && ~target.indexOf(',')))
126
- }
127
-
128
- // when root level is 0, merge the navigation into the rootEntry
129
- // when root level is 1, create navigation per navigation menu
130
- // in this case, if there's only a single navigation menu with no title, promote each top-level item to a menu
131
- function prepareOutlines (navigation, rootEntry, rootLevel) {
132
- if (rootLevel === 0 || navigation.length === 1) {
133
- let navBranch
134
- if (navigation.length === 1) {
135
- navBranch = navigation[0]
136
- } else {
137
- const items = navigation.reduce((accum, it) => accum.concat(it.content ? it : it.items), [])
138
- navBranch = items.length ? { items } : {}
139
- }
140
- return rootLevel === 0 || navBranch.content ? [Object.assign(rootEntry, navBranch)] : navBranch.items
141
- }
142
- return navigation.reduce((navTree, it) => navTree.concat(it.content ? it : it.items), [rootEntry])
143
- }
144
-
145
- module.exports = produceAggregateDocuments
@@ -1,60 +0,0 @@
1
- 'use strict'
2
-
3
- const fs = require('node:fs')
4
- const LazyReadable = require('./lazy-readable')
5
- const { spawn } = require('node:child_process')
6
-
7
- const IS_WIN = process.platform === 'win32'
8
- const DBL_QUOTE_RX = /"/g
9
-
10
- const shellEscape = IS_WIN
11
- ? (val) => {
12
- if (val.charAt() === '-') {
13
- return val
14
- } else if (~val.indexOf('"')) {
15
- return ~val.indexOf(' ') ? `"${val.replace(DBL_QUOTE_RX, '"""')}"` : val.replace(DBL_QUOTE_RX, '""')
16
- } else if (~val.indexOf(' ')) {
17
- return `"${val}"`
18
- }
19
- return val
20
- }
21
- : (val) => val
22
-
23
- async function runCommand (cmd, argv = [], opts = {}) {
24
- if (!cmd) throw new TypeError('Command not specified')
25
- const cmdv = cmd.split(' ').map(shellEscape)
26
- const { input, output, implicitStdin, ...spawnOpts } = opts
27
- if (input) input instanceof Buffer ? implicitStdin || argv.push('-') : argv.push(input)
28
- if (IS_WIN) Object.assign(spawnOpts, { shell: true, windowsHide: true })
29
- return new Promise((resolve, reject) => {
30
- const stdout = []
31
- const stderr = []
32
- const ps = spawn(cmdv[0], [...cmdv.slice(1), ...argv.map(shellEscape)], spawnOpts)
33
- ps.on('close', (code) => {
34
- if (code === 0) {
35
- if (stderr.length) process.stderr.write(stderr.join(''))
36
- resolve(output ? new LazyReadable(() => fs.createReadStream(output)) : Buffer.from(stdout.join('')))
37
- } else {
38
- let msg = `Command failed: ${ps.spawnargs.join(' ')}`
39
- if (stderr.length) msg += '\n' + stderr.join('')
40
- reject(new Error(msg))
41
- }
42
- })
43
- ps.on('error', (err) => reject(err.code === 'ENOENT' ? new Error(`Command not found: ${cmdv.join(' ')}`) : err))
44
- ps.stdout.on('data', (data) => (output ? process.stdout.write(data) : stdout.push(data)))
45
- ps.stderr.on('data', (data) => stderr.push(data))
46
- if (input instanceof Buffer) {
47
- try {
48
- ps.stdin.on('error', () => undefined)
49
- ps.stdin.end(input)
50
- } catch (err) {
51
- if (!ps.stdin.writableEnded) ps.stdin.end()
52
- reject(err)
53
- }
54
- } else {
55
- ps.stdin.end()
56
- }
57
- })
58
- }
59
-
60
- module.exports = runCommand