diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 8d30e5a349fa699c9d617051d20e1593b94b978b..da97c34fec30ce1f04ffa22eb5b970b8d69e7603 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -6,9 +6,14 @@ For a detailed view of what's changed, refer to the {url-repo}/commits[commit hi == Unreleased +=== Change + +* log warning message if include directive to reduce cannot be found + === Fixed * reify leveloffset to handle cases when leveloffset is set outside of document header +* properly reduce include directives that use the `leveloffset` attribute (#9) == 1.0.0-alpha.5 (2022-07-24) diff --git a/packages/assembler/lib/asciidoctor/reducer-extension.js b/packages/assembler/lib/asciidoctor/reducer-extension.js index 064812ce0cfec731b8725133ea167c979780f5b6..0faaa3b080efffc775b779f9a23acdd11fd8d6b4 100644 --- a/packages/assembler/lib/asciidoctor/reducer-extension.js +++ b/packages/assembler/lib/asciidoctor/reducer-extension.js @@ -121,6 +121,7 @@ const IncludeDirectiveTracker = (() => { this.$$reducer.includePushed = true const directiveLineno = this.lineno - 1 // we're below the include line, which is 1-based const prevIncDepth = this.include_stack.length + let offset = lineno > 1 ? lineno - 1 : 0 const result = Opal.send(this, Opal.find_super_dispatcher(this, 'push_include', pushInclude), [ data, file, @@ -128,12 +129,12 @@ const IncludeDirectiveTracker = (() => { lineno, attrs, ]) - pushIncludeReplacement.call( - this, - directiveLineno, - this.include_stack.length > prevIncDepth ? this.$lines() : [], - lineno > 1 ? lineno - 1 : 0 - ) + let incLines = [] + if (this.include_stack.length > prevIncDepth) { + incLines = this.$lines() + if (attrs['$key?']('leveloffset') && incLines[0].startsWith(':leveloffset: ') && incLines[1] === '') offset -= 2 + } + pushIncludeReplacement.call(this, directiveLineno, incLines, offset) return result }) @@ -174,8 +175,12 @@ function treeProcessor () { let targetLines, idx if (into != null) { targetLines = incReplacements[into].lines - // adds assurance that we're replacing the correct line - if (targetLines[(idx = lineno - 1)] !== line) return + // adds extra assurance that the program is replacing the correct line + if (targetLines[(idx = lineno - 1)] !== line) { + const msg = `include directive to reduce not found; expected: "${line}"; got: "${targetLines[idx]}"` + doc.getLogger().error(msg) + return + } } if ((drop || []).length) { drop diff --git a/packages/assembler/test/produce-aggregate-documents-test.js b/packages/assembler/test/produce-aggregate-documents-test.js index 4a26506550b8a8186513248206afc6a239b74b0e..9bd49ad208e4a6a8d6543ac2cbab94d23f4bc489 100644 --- a/packages/assembler/test/produce-aggregate-documents-test.js +++ b/packages/assembler/test/produce-aggregate-documents-test.js @@ -74,6 +74,10 @@ describe('produceAggregateDocuments()', () => { await runScenario('expand-nested-include', __dirname) }) + it('should expand nested include with leveloffset', async () => { + await runScenario('expand-nested-include-leveloffset', __dirname) + }) + it('should evaluate preprocessor conditional', async () => { await runScenario('evaluate-preprocessor-conditional', __dirname) }) diff --git a/packages/assembler/test/scenarios/expand-nested-include-leveloffset/data.yml b/packages/assembler/test/scenarios/expand-nested-include-leveloffset/data.yml new file mode 100644 index 0000000000000000000000000000000000000000..21f162026846b32d119b758ecd0f8501bd140618 --- /dev/null +++ b/packages/assembler/test/scenarios/expand-nested-include-leveloffset/data.yml @@ -0,0 +1,33 @@ +name: the-component +version: '1.0' +title: The Component +navigation: + items: + - content: xref:the-page.adoc[The Page Title] +files: +- relative: the-page.adoc + contents: |- + = The Page Title + + == Section + + include::_sections.adoc[leveloffset=+1] + + == Another Section +- relative: _sections.adoc + contents: |- + == Subsection + + include::_content.adoc[] + + === Nested Subsection + + include::_more-content.adoc[] +- relative: _content.adoc + contents: |- + A paragraph. +- relative: _more-content.adoc + contents: |- + Another paragraph. + + And yet another. diff --git a/packages/assembler/test/scenarios/expand-nested-include-leveloffset/expects/the-component.adoc b/packages/assembler/test/scenarios/expand-nested-include-leveloffset/expects/the-component.adoc new file mode 100644 index 0000000000000000000000000000000000000000..4f337c5dee7972bc317ee132167abb29ad08835d --- /dev/null +++ b/packages/assembler/test/scenarios/expand-nested-include-leveloffset/expects/the-component.adoc @@ -0,0 +1,37 @@ += The Component +v1.0 +:doctype: book +:page-component-name: the-component +:page-component-version: 1.0 +:page-version: {page-component-version} +:page-component-display-version: 1.0 +:page-component-title: The Component + +:docname: the-page +:page-module: ROOT +:page-relative-src-path: the-page.adoc +:page-origin-url: https://github.com/acme/the-component +:page-origin-start-path: +:page-origin-refname: v1.0 +:page-origin-reftype: branch +:page-origin-refhash: a00000000000000000000000000000000000000z +[#the-page:::] +== The Page Title + +[discrete#the-page:::_section] +=== Section + +[discrete#the-page:::_subsection] +==== Subsection + +A paragraph. + +[discrete#the-page:::_nested_subsection] +===== Nested Subsection + +Another paragraph. + +And yet another. + +[discrete#the-page:::_another_section] +=== Another Section