diff --git a/lib/static-page-extension.js b/lib/static-page-extension.js new file mode 100644 index 0000000..ccf5d37 --- /dev/null +++ b/lib/static-page-extension.js @@ -0,0 +1,23 @@ +'use strict' + +const fsp = require('node:fs/promises') +const resolvedSearch = require.resolve('@springio/antora-extensions/static-pages/search') + +module.exports.register = function () { + this.once('contentAggregated', async ({ contentAggregate }) => { + for (const componentVersionBucket of contentAggregate) { + const searchFile = { + path: 'modules/ROOT/pages/search.adoc', + contents: Buffer.from(await fsp.readFile(resolvedSearch, 'utf8')), + src: { + path: 'modules/ROOT/pages/search.adoc', + basename: 'search.adoc', + stem: 'search', + extname: '.adoc', + abspath: resolvedSearch, + }, + } + componentVersionBucket.files.push(searchFile) + } + }) +} diff --git a/lib/static/search.adoc b/lib/static/search.adoc new file mode 100644 index 0000000..0586639 --- /dev/null +++ b/lib/static/search.adoc @@ -0,0 +1,4 @@ += Search +:page-article: search + +Search in all Spring Docs \ No newline at end of file diff --git a/package.json b/package.json index 111a19f..a6fe2f1 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,9 @@ "./publish-docsearch-config-extension": "./lib/publish-docsearch-config-extension.js", "./root-component-extension": "./lib/root-component-extension.js", "./tabs-migration-extension": "./lib/tabs-migration-extension.js", - "./cache-scandir": "./lib/cache-scandir/index.js" + "./static-page-extension": "./lib/static-page-extension.js", + "./cache-scandir": "./lib/cache-scandir/index.js", + "./static-pages/search": "./lib/static/search.adoc" }, "imports": { "#package": "./package.json" diff --git a/test/static-page-extension-test.js b/test/static-page-extension-test.js new file mode 100644 index 0000000..2b261ff --- /dev/null +++ b/test/static-page-extension-test.js @@ -0,0 +1,81 @@ +/* eslint-env mocha */ +'use strict' + +const { expect } = require('./harness') +const { name: packageName } = require('#package') +const resolvedSearch = require.resolve('@springio/antora-extensions/static-pages/search') +describe('static-page-extension', () => { + const ext = require(packageName + '/static-page-extension') + const createGeneratorContext = () => ({ + messages: [], + variables: {}, + once (eventName, fn) { + this[eventName] = fn + }, + on (eventName, fn) { + this[eventName] = fn + }, + require, + getLogger (name) { + const messages = this.messages + const appendMessage = function (message) { + messages.push(message) + } + return { + info: appendMessage, + debug: appendMessage, + } + }, + updateVariables (updates) { + Object.assign(this.variables, updates) + }, + }) + + let contentAggregate + let generatorContext + let playbook + + beforeEach(async () => { + generatorContext = createGeneratorContext() + contentAggregate = [{ files: [] }] + }) + + describe('bootstrap', () => { + it('should be able to require extension', () => { + expect(ext).to.be.instanceOf(Object) + expect(ext.register).to.be.instanceOf(Function) + }) + + it('should be able to call register function exported by extension', () => { + ext.register.call(generatorContext, { config: {}, playbook }) + expect(generatorContext.contentAggregated).to.be.instanceOf(Function) + }) + }) + + describe('contentAggregate', () => { + it('adds file', async () => { + ext.register.call(generatorContext, {}) + await generatorContext.contentAggregated({ contentAggregate }) + const search = contentAggregate[0].files[0] + // convert contents to a String so it can be compared + search.contents = search.contents.toString() + expect(contentAggregate).to.eql([ + { + files: [ + { + contents: '= Search\n:page-article: search\n\nSearch in all Spring Docs', + path: 'modules/ROOT/pages/search.adoc', + src: { + abspath: resolvedSearch, + basename: 'search.adoc', + extname: '.adoc', + path: 'modules/ROOT/pages/search.adoc', + stem: 'search', + }, + }, + ], + }, + ]) + }) + }) +})