From f7b41e7480f35ad207e0ae1d413d30eab1c701d9 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sat, 16 Mar 2024 02:08:39 -0600 Subject: [PATCH] resolves #45 convert reference to attachment in nav to external link if site URL is specified --- CHANGELOG.adoc | 1 + .../lib/produce-aggregate-document.js | 16 ++++++++---- .../test/produce-aggregate-documents-test.js | 4 +++ .../scenarios/attachment-reference/data.yml | 19 ++++++++++++++ .../expects/the-component.adoc | 26 +++++++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 packages/assembler/test/scenarios/attachment-reference/data.yml create mode 100644 packages/assembler/test/scenarios/attachment-reference/expects/the-component.adoc diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 259588f..9ee7adf 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -23,6 +23,7 @@ For a detailed view of what's changed, refer to the {url-repo}/commits[commit hi * add default text to link that points to page outside of the aggregate document * scope IDs for pages not from current component (#37) * don't insert start page if it points outside current component version (#44) +* convert reference to attachment in nav to external link if site URL is specified (#45) === Changed diff --git a/packages/assembler/lib/produce-aggregate-document.js b/packages/assembler/lib/produce-aggregate-document.js index 0baaba3..002f95f 100644 --- a/packages/assembler/lib/produce-aggregate-document.js +++ b/packages/assembler/lib/produce-aggregate-document.js @@ -460,6 +460,7 @@ function aggregateAsciiDoc ( } else { buffer.push('') // NOTE: try to toggle sectids; otherwise, fallback to globally unique synthetic ID + // Q: should we unset docname, page-module, etc? let toggleSectids, syntheticId if (!('sectids' in asciidocConfig.attributes)) { buffer.push(':!sectids:') @@ -472,11 +473,16 @@ function aggregateAsciiDoc ( syntheticId = `__object-id-${global.Opal.hash(outlineEntry).$object_id()}` } } - // Q: should we unset docname, page-module, etc? - const sectionTitle = - urlType === 'external' - ? `${url}[${navtitlePass ? navtitleAsciiDoc : navtitleAsciiDoc.replace(/\]/g, '\\]')}]` - : navtitleAsciiDoc + let sectionTitle = navtitleAsciiDoc + if (urlType === 'external') { + sectionTitle = `${url}[${navtitlePass ? navtitleAsciiDoc : navtitleAsciiDoc.replace(/\]/g, '\\]')}]` + } else if (urlType === 'internal' && !unresolved && siteUrl) { + const resource = contentCatalog.getFiles().find((it) => it.pub.url === url) + if (resource?.out) { + sectionTitle = navtitlePass ? navtitleAsciiDoc : navtitleAsciiDoc.replace(/\]/g, '\\]') + sectionTitle = `${siteUrl}${resource.pub.url}[${sectionTitle}]` + } + } let hlevel = level + 1 if (hlevel > 6) { hlevel = 6 diff --git a/packages/assembler/test/produce-aggregate-documents-test.js b/packages/assembler/test/produce-aggregate-documents-test.js index 3cf2341..8bcc523 100644 --- a/packages/assembler/test/produce-aggregate-documents-test.js +++ b/packages/assembler/test/produce-aggregate-documents-test.js @@ -27,6 +27,10 @@ describe('produceAggregateDocuments()', () => { await runScenario('ignore-duplicate-nav-entry', __dirname) }) + it('should handle navigation entry with attachment reference', async () => { + await runScenario('attachment-reference', __dirname) + }) + it('should handle navigation entry with external link', async () => { await runScenario('external-link', __dirname) }) diff --git a/packages/assembler/test/scenarios/attachment-reference/data.yml b/packages/assembler/test/scenarios/attachment-reference/data.yml new file mode 100644 index 0000000..35d3475 --- /dev/null +++ b/packages/assembler/test/scenarios/attachment-reference/data.yml @@ -0,0 +1,19 @@ +name: the-component +version: '1.0' +title: The Component +navigation: + items: + - content: xref:the-page.adoc[] + - content: xref:attachment$form.pdf[Form Title] +files: +- relative: the-page.adoc + contents: |- + = The Page Title + + contents +- relative: form.pdf + family: attachment +assembler: + asciidoc: + attributes: + site-url: https://docs.example.org diff --git a/packages/assembler/test/scenarios/attachment-reference/expects/the-component.adoc b/packages/assembler/test/scenarios/attachment-reference/expects/the-component.adoc new file mode 100644 index 0000000..ad38039 --- /dev/null +++ b/packages/assembler/test/scenarios/attachment-reference/expects/the-component.adoc @@ -0,0 +1,26 @@ += The Component +:revnumber: 1.0 +:doctype: book +:underscore: _ +: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 + +contents + +:!sectids: +== https://docs.example.org/the-component/1.0/_attachments/form.pdf[Form Title] +:sectids: -- GitLab