Skip to content

Commit

Permalink
Add Static Search Page
Browse files Browse the repository at this point in the history
Closes gh-14
  • Loading branch information
rwinch committed Sep 20, 2023
1 parent fc62344 commit c6b86d9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/static-page-extension.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
4 changes: 4 additions & 0 deletions lib/static/search.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Search
:page-article: search

Search in all Spring Docs
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
81 changes: 81 additions & 0 deletions test/static-page-extension-test.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
],
},
])
})
})
})

0 comments on commit c6b86d9

Please sign in to comment.