@feelpp/antora-extensions 1.0.0-rc.2 → 1.0.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,62 @@
1
1
  # Antora Extensions by Feel++
2
2
 
3
+ ![NPM Version](https://img.shields.io/npm/v/%40feelpp%2Fantora-extensions)
4
+
3
5
  A set of Antora extensions.
6
+
7
+ ## Extensions
8
+
9
+ ### Lunr Search Index
10
+
11
+ Automatically generates a Lunr.js compatible search index during the Antora build process.
12
+
13
+ #### Usage
14
+
15
+ Add the extension to your `site.yml`:
16
+
17
+ ```yaml
18
+ antora:
19
+ extensions:
20
+ - '@feelpp/antora-extensions'
21
+
22
+ config:
23
+ lunr:
24
+ indexFile: 'search-index.json' # Output filename (optional)
25
+ maxContentLength: 1000 # Max content per document (optional)
26
+ minContentLength: 50 # Min content to include document (optional)
27
+ debug: false # Enable debug logging (optional)
28
+ ```
29
+
30
+ The search index will be automatically generated at `build/site/search-index.json` after the site is built.
31
+
32
+ #### Features
33
+
34
+ - **Zero configuration**: Works out of the box with sensible defaults
35
+ - **Smart content extraction**: Focuses on main content, excludes navigation and footers
36
+ - **Configurable**: Customize content length limits and output location
37
+ - **Performance optimized**: Efficient processing during site generation
38
+ - **Error handling**: Graceful handling of malformed HTML or missing content
39
+
40
+ ## Listing
41
+
42
+ ### UI assumptions
43
+
44
+ This extension relies on a contract with the UI in order to minimize the configuration the user must perform to get the extension working.
45
+
46
+ #### Environment variable
47
+
48
+ When this extension is enabled, it sets the `SITE_LISTING_EXTENSION_ENABLED` environment variable to the value `true`.
49
+ This variable is available to the UI templates as `env.SITE_LISTING_EXTENSION_ENABLED`.
50
+ The existence of this variable informs the UI template that the listing extension is enabled.
51
+ When this variable is set, the UI is expected to add certain elements to support the extension.
52
+
53
+ #### Listing styles
54
+
55
+ This package provides additional CSS to style the listing results (`data/css/listing.css`).
56
+ The creator of the UI can either bundle those styles or reference them (for instance in `head-styles.hbs`):
57
+
58
+ ```hbs
59
+ {{#if env.SITE_LISTING_EXTENSION_ENABLED}}
60
+ <link rel="stylesheet" href="{{{uiRootPath}}}/css/listing.css">
61
+ {{/if}}
62
+ ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@feelpp/antora-extensions",
3
3
  "private": false,
4
- "version": "1.0.0-rc.2",
4
+ "version": "1.0.0-rc.3",
5
5
  "description": "Antora Extensions by Feel++.",
6
6
  "main": "src/index.js",
7
7
  "repository": {
@@ -11,12 +11,20 @@
11
11
  "keywords": [],
12
12
  "author": "Guillaume Grossetie <ggrossetie@yuzutech.fr>",
13
13
  "license": "MIT",
14
+ "scripts": {
15
+ "test": "mocha tests/*-test.js"
16
+ },
14
17
  "bugs": {
15
18
  "url": "https://github.com/feelpp/feelpp-antora-extensions/issues"
16
19
  },
17
20
  "homepage": "https://github.com/feelpp/feelpp-antora-extensions#readme",
18
21
  "devDependencies": {
22
+ "chai": "^4.3.7",
23
+ "mocha": "^10.2.0",
19
24
  "libnpmpublish": "^4.0.2",
20
25
  "pacote": "^12.0.2"
26
+ },
27
+ "dependencies": {
28
+ "jsdom": "^20.0.3"
21
29
  }
22
30
  }
package/src/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  const {register: registerTooboxNavigation} = require('./toolbox-navigation.js')
2
2
  const {register: registerListing} = require('./listing.js')
3
+ const {register: registerLunr} = require('./lunr.js')
3
4
 
4
5
  module.exports.register = (thisContext, args) => {
5
6
  registerTooboxNavigation.call(thisContext, args)
6
7
  registerListing.call(thisContext, args)
8
+ registerLunr.call(thisContext, args)
7
9
  }
package/src/lunr.js ADDED
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Antora extension for automatic Lunr.js search index generation
3
+ *
4
+ * This extension automatically generates a Lunr.js compatible search index
5
+ * after the site is published. It extracts content from all HTML files
6
+ * and creates a JSON index that can be used for client-side search.
7
+ */
8
+
9
+ const fs = require('fs')
10
+ const path = require('path')
11
+
12
+ // Lazy load JSDOM only when needed
13
+ let JSDOM
14
+ function loadJSDOM() {
15
+ if (!JSDOM) {
16
+ try {
17
+ JSDOM = require('jsdom').JSDOM
18
+ } catch (error) {
19
+ throw new Error('JSDOM is required for the Lunr extension. Install it with: npm install jsdom')
20
+ }
21
+ }
22
+ return JSDOM
23
+ }
24
+
25
+ function register({ config, playbook }) {
26
+ const logger = this.getLogger('lunr-extension')
27
+
28
+ this.once('sitePublished', (event) => {
29
+ logger.info('Generating Lunr.js search index...')
30
+
31
+ try {
32
+ // Get output directory from the playbook
33
+ const outputDir = playbook.output?.dir || 'build/site'
34
+ logger.info(`Output directory: ${outputDir}`)
35
+
36
+ generateSearchIndex(outputDir, config.lunr || {}, logger)
37
+ } catch (error) {
38
+ logger.error('Failed to generate search index:', error)
39
+ throw error
40
+ }
41
+ })
42
+ }
43
+
44
+ function generateSearchIndex(outputDir, config = {}, logger) {
45
+ const searchIndexFile = path.join(outputDir, config.indexFile || 'search-index.json')
46
+ const maxContentLength = config.maxContentLength || 1000
47
+ const minContentLength = config.minContentLength || 50
48
+
49
+ // Find all HTML files
50
+ const htmlFiles = walkDirectory(outputDir).filter(file => file.endsWith('.html'))
51
+ const documents = []
52
+ let id = 0
53
+
54
+ htmlFiles.forEach(filePath => {
55
+ try {
56
+ const html = fs.readFileSync(filePath, 'utf8')
57
+ const title = getTitle(html)
58
+ const content = extractTextContent(html)
59
+
60
+ // Skip empty content
61
+ if (!content || content.length < minContentLength) {
62
+ return
63
+ }
64
+
65
+ // Generate relative URL
66
+ const relativePath = path.relative(outputDir, filePath)
67
+ const url = '/' + relativePath.replace(/\\/g, '/').replace(/\/index\.html$/, '/')
68
+
69
+ documents.push({
70
+ id: ++id,
71
+ title: title,
72
+ content: content.substring(0, maxContentLength),
73
+ url: url
74
+ })
75
+
76
+ if (config.debug) {
77
+ logger.debug(`Indexed: ${title} (${url})`)
78
+ }
79
+ } catch (error) {
80
+ logger.warn(`Failed to process ${filePath}:`, error.message)
81
+ }
82
+ })
83
+
84
+ const searchIndex = {
85
+ documents: documents
86
+ }
87
+
88
+ // Write search index
89
+ fs.writeFileSync(searchIndexFile, JSON.stringify(searchIndex, null, 2))
90
+ logger.info(`Search index generated: ${documents.length} documents → ${searchIndexFile}`)
91
+ }
92
+
93
+ function extractTextContent(html) {
94
+ try {
95
+ const JSDOM = loadJSDOM()
96
+ const dom = new JSDOM(html)
97
+ const document = dom.window.document
98
+
99
+ // Remove script and style elements
100
+ const scripts = document.querySelectorAll('script, style, nav, .navbar, .footer')
101
+ scripts.forEach(el => el.remove())
102
+
103
+ // Get main content area
104
+ const main = document.querySelector('main, .main, .content, article')
105
+ const content = main || document.body
106
+
107
+ return content.textContent.trim().replace(/\s+/g, ' ')
108
+ } catch (error) {
109
+ return ''
110
+ }
111
+ }
112
+
113
+ function getTitle(html) {
114
+ try {
115
+ const JSDOM = loadJSDOM()
116
+ const dom = new JSDOM(html)
117
+ const titleEl = dom.window.document.querySelector('title, h1, .page-title')
118
+ return titleEl ? titleEl.textContent.trim() : 'Untitled'
119
+ } catch (error) {
120
+ return 'Untitled'
121
+ }
122
+ }
123
+
124
+ function walkDirectory(dir, fileList = []) {
125
+ if (!fs.existsSync(dir)) {
126
+ return fileList
127
+ }
128
+
129
+ const files = fs.readdirSync(dir)
130
+
131
+ files.forEach(file => {
132
+ const filePath = path.join(dir, file)
133
+ const stat = fs.statSync(filePath)
134
+
135
+ if (stat.isDirectory()) {
136
+ walkDirectory(filePath, fileList)
137
+ } else {
138
+ fileList.push(filePath)
139
+ }
140
+ })
141
+
142
+ return fileList
143
+ }
144
+
145
+ module.exports = { register }
@@ -0,0 +1,36 @@
1
+ const { describe, it } = require('mocha')
2
+ const { expect } = require('chai')
3
+ const fs = require('fs')
4
+ const path = require('path')
5
+
6
+ describe('Lunr extension', () => {
7
+ const { register } = require('../src/lunr.js')
8
+
9
+ it('should export a register function', () => {
10
+ expect(register).to.be.a('function')
11
+ })
12
+
13
+ it('should register with Antora context', () => {
14
+ let eventRegistered = false
15
+ const mockContext = {
16
+ getLogger: () => ({
17
+ info: () => {},
18
+ error: () => {},
19
+ debug: () => {},
20
+ warn: () => {}
21
+ }),
22
+ once: (event, handler) => {
23
+ if (event === 'sitePublished') {
24
+ eventRegistered = true
25
+ }
26
+ }
27
+ }
28
+
29
+ const config = {}
30
+ const playbook = { output: { dir: 'build/site' } }
31
+
32
+ register.call(mockContext, { config, playbook })
33
+
34
+ expect(eventRegistered).to.be.true
35
+ })
36
+ })