From 3b638c66263426a008e744b7b65961ba36ce7ff0 Mon Sep 17 00:00:00 2001 From: Neil Dewhurst Date: Mon, 29 Jan 2024 11:21:40 +0000 Subject: [PATCH] Fix logging and warnings (#16) --- .../aliases-redirects/aliases-redirects.js | 48 ++++++++++--------- .../antora/aliases-redirects/package.json | 2 +- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/extensions/antora/aliases-redirects/aliases-redirects.js b/extensions/antora/aliases-redirects/aliases-redirects.js index fb904a3..26cfc58 100644 --- a/extensions/antora/aliases-redirects/aliases-redirects.js +++ b/extensions/antora/aliases-redirects/aliases-redirects.js @@ -5,7 +5,7 @@ module.exports.register = function ({ config }) { const { playbook } = this.getVariables() - const { redirectFormat = 'neo4j', aliasLogLevel = playbook.asciidoc.attributes.aliasLogLevel || 'info', logFoundAliases = playbook.asciidoc.attributes.logFoundAliases || false } = config + const { redirectFormat = 'neo4j', aliasLogLevel = playbook.asciidoc.attributes.aliasLogLevel || 'warn', logFoundAliases = playbook.asciidoc.attributes.logFoundAliases || false } = config const pluralize = (count, noun, suffix = 's', plural = '') => { if (count !== 1) { @@ -38,6 +38,8 @@ module.exports.register = function ({ config }) { const { contentCatalog } = this.getVariables() + const allAliases = contentCatalog.findBy({ family: 'alias' }) + contentCatalog.getComponents().forEach(({ name, versions }) => { const componentName = name @@ -70,7 +72,6 @@ module.exports.register = function ({ config }) { // logger.warn({ }, 'Found %d %s in %s', allPages.oldPages.length, pluralize(allPages.oldPages.length, 'page'), redirectFromVersion) // logger.warn({ }, 'Found %d %s in %s', allPages.newPages.length, pluralize(allPages.newPages.length, 'page'), redirectToVersion) - const newSources = allPages.newPages.reduce((entries, page) => { if (!page.out) { @@ -83,8 +84,7 @@ module.exports.register = function ({ config }) { return entries }, [ ]) - - + // what's missing in the new version? allPages.oldPages.forEach( (page) => { @@ -94,9 +94,18 @@ module.exports.register = function ({ config }) { // - the source path is the same (ie the same .adoc file exists in both versions) // - the output url is the same (ie file.adoc exists in one version and file/index.adoc exists in the other version) const newPage = newSources.find(({ path, url }) => path === page.src.path || url === page.pub.url.replace(redirectFromVersion, redirectToVersion) ); + + // return if the page exists in the new version if (newPage) { return } + + // return if the page has a page-external attribute, indicating that it has been moved outside this component + // TODO - add a redirect to the external page URL + if (page.asciidoc.attributes['page-external']) { + if (logFoundAliases) logger.info({ 'file': page.src, 'source': page.src.origin }, 'Page %s (%s) has been moved to %s', page.src.relative, page.asciidoc.doctitle, page.asciidoc.attributes['page-external']) + return + } // logger.warn({ 'file': page.src, 'source': page.src.origin }, 'Page in version %s removed or moved in later versions: %s (%s)', page.src.version, page.src.path, page.asciidoc.doctitle) @@ -105,29 +114,24 @@ module.exports.register = function ({ config }) { // First check if there are any page aliases that point to this page // If there is at least one, we don't need to do anything, that alias means there will be no 404 in the new version for this page const requiredAliasTarget = page.pub.url.replace(redirectFromVersion, redirectToVersion) - const allAliases = contentCatalog.findBy({ family: 'alias' }) - const aliasesToHere = allAliases.filter(alias => alias.pub.url === requiredAliasTarget) + const aliasesToHere = allAliases.filter(alias => alias.pub.url === ensureTrailingSlash(requiredAliasTarget)) // alias.pub.url is constructed from the page-alias value // alias.rel.pub.url is constructed from the output path of the file that contains the page-alias - if (aliasesToHere.length > 0 && logFoundAliases) { - aliasesToHere.forEach(alias => { - // console.log(alias.rel.pub) - // console.log(alias.pub) - // console.log(alias.src) - // logger.info({ 'file': page.src, 'source': page.src.origin }, 'Alias found for page %s (%s)', redirectFromVersion, page.src.path, page.asciidoc.doctitle) - logger.info({ 'file': page.src, 'source': page.src.origin }, 'Alias %s found for %s (%s) which was removed from %s to %s', alias.rel.pub.url, page.src.relative, page.asciidoc.doctitle, redirectFromVersion, redirectToVersion) - }) + if (aliasesToHere.length > 0) { + if (logFoundAliases) { + aliasesToHere.forEach(alias => { + // console.log(alias.rel.pub) + // console.log(alias.pub) + // console.log(alias.src) + // logger[aliasLogLevel]({ 'file': page.src, 'source': page.src.origin }, 'Alias found for page %s (%s)', redirectFromVersion, page.src.path, page.asciidoc.doctitle) + logger.info({ 'file': page.src, 'source': page.src.origin }, 'Alias %s found for %s (%s) which was removed after %s', alias.rel.pub.url, page.src.relative, page.asciidoc.doctitle, redirectFromVersion) + }) + } return // nothing to do here if there is at least one alias to this page } - // return if the page has a page-moved-to attribute, indicating that it has been moved outside this component - if (page.asciidoc.attributes['page-moved-to'] && logFoundAliases) { - logger.info({ 'file': page.src, 'source': page.src.origin }, 'Page %s (%s) has been moved to %s', page.src.relative, page.asciidoc.doctitle, page.asciidoc.attributes['page-moved-to']) - return - } - // the path we need to redirect from is the old path but with the new version // const redirectFromPath = page.out.dirname.replace(redirectFromVersion, redirectToVersion) @@ -150,11 +154,11 @@ module.exports.register = function ({ config }) { redirectTo.forEach( (target, i) => { const newFile = contentCatalog.getByPath({component: target.component, version: target.version, path: target.path}) // if (i ==0) contentCatalog.registerPageAlias(page.src.relative,newFile) - logger.info({ 'file': newFile.src, 'source': newFile.src.origin }, '%s is a possible alias found for %s (%s) which was removed from %s to %s', newFile.src.relative, page.src.relative, searchTitle, redirectFromVersion, redirectToVersion) + logger[aliasLogLevel]({ 'file': newFile.src, 'source': newFile.src.origin }, '%s (%s) was removed after %s. A possible alias in %s is: %s', page.src.relative, searchTitle, redirectFromVersion, redirectToVersion, newFile.src.relative) }) } else { - logger[aliasLogLevel]({ 'file': page.out.dirname, 'source': page.src.origin }, 'No aliases found for %s (%s) which was removed in version %s', page.src.path, searchTitle, redirectToVersion) + logger[aliasLogLevel]({ 'file': page.src, 'source': page.src.origin }, 'No aliases found for %s (%s) which was removed in version %s', page.src.path, searchTitle, redirectToVersion) } }) diff --git a/extensions/antora/aliases-redirects/package.json b/extensions/antora/aliases-redirects/package.json index 09ca1ba..81e5b74 100644 --- a/extensions/antora/aliases-redirects/package.json +++ b/extensions/antora/aliases-redirects/package.json @@ -1,6 +1,6 @@ { "name": "@neo4j-antora/aliases-redirects", - "version": "0.2.3", + "version": "0.2.4", "description": "Help with redirects and page aliases", "main": "aliases-redirects.js", "scripts": {