Skip to content

Commit 08867b4

Browse files
authoredMay 23, 2022
Bump antora from 2.3 to 3.0 (#92)
* Bump antora from 2.3 to 3.0 * Update antora-lunr to lunr-extension * Bump asciidoctor-kroki to 0.15.4 * Bump asciidoctor-mathjax to 0.0.5 * Fix build script for Netlify
1 parent a0e7cd3 commit 08867b4

File tree

11 files changed

+1115
-4613
lines changed

11 files changed

+1115
-4613
lines changed
 

‎antora-playbook.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ content:
1616
branches: develop
1717
ui:
1818
bundle:
19-
url: https://github.com/decidim/documentation-antora-ui/releases/download/v0.3/ui-bundle.zip
19+
url: https://github.com/decidim/documentation-antora-ui/releases/download/v0.4/ui-bundle.zip
2020
snapshot: true
2121
supplemental_files: ./supplemental_ui
22+
antora:
23+
extensions:
24+
- require: '@antora/lunr-extension'
2225
asciidoc:
2326
attributes:
2427
kroki-fetch-diagram: true

‎bin/dbuild

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/env bash
22

3-
docker run -u $(id -u) -e DOCSEARCH_ENABLED=true -e DOCSEARCH_ENGINE=lunr -v $PWD:/antora:Z --rm -t antora/antora --generator antora-site-generator-lunr --cache-dir=./.cache/antora antora-playbook.yml $@
3+
docker run -u $(id -u) -v $PWD:/antora:Z --rm -t antora/antora --cache-dir=./.cache/antora antora-playbook.yml $@
44

‎netlify.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build]
22
publish = "build/site/"
3-
command = "node_modules/.bin/antora --generator antora-site-generator-lunr --stacktrace --fetch --html-url-extension-style=indexify antora-playbook.yml"
3+
command = "npm run build"
44

55
[build.environment]
66
NODE_VERSION = "14"
@@ -10,7 +10,7 @@
1010
DOCSEARCH_ENGINE = "lunr"
1111

1212
[context.deploy-preview]
13-
command = "sed -i'' -e 's;branches: master;branches: HEAD;g' antora-playbook.yml && node_modules/.bin/antora --generator antora-site-generator-lunr --stacktrace --fetch --html-url-extension-style=indexify --url $DEPLOY_PRIME_URL antora-playbook.yml"
13+
command = "sed -i'' -e 's;branches: master;branches: HEAD;g' antora-playbook.yml && npm run build"
1414

1515
[[redirects]]
1616
from = "/social-contract/en/*"

‎package-lock.json

+1,008-988
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
"name": "decidim-documentation",
33
"private": true,
44
"scripts": {
5-
"build": "DOCSEARCH_ENABLED=true DOCSEARCH_ENGINE=lunr antora --generator antora-site-generator-lunr antora-playbook.yml $@"
5+
"build": "antora antora-playbook.yml $@"
66
},
77
"devDependencies": {
8-
"@antora/cli": "^2.3.0",
9-
"@antora/site-generator-default": "^2.3.0"
8+
"@antora/cli": "^3.0.0",
9+
"@antora/site-generator-default": "^3.0.0"
1010
},
1111
"dependencies": {
12-
"antora-lunr": "^0.7.1",
13-
"antora-site-generator-lunr": "^0.5.0",
14-
"asciidoctor-kroki": "^0.9.2",
12+
"@antora/lunr-extension": "^1.0.0-alpha.6",
13+
"asciidoctor-kroki": "^0.15.4",
1514
"asciidoctor-mathjax": "0.0.5"
1615
}
1716
}

‎supplemental_ui/css/search.css

+15-55
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,3 @@
1-
.navbar-brand .navbar-item + .navbar-item {
2-
flex-grow: 1;
3-
justify-content: flex-end;
4-
}
5-
6-
@media screen and (min-width: 1024px) {
7-
.navbar-brand {
8-
flex-grow: 1;
9-
}
10-
11-
.navbar-menu {
12-
flex-grow: 0;
13-
}
14-
}
15-
16-
#search-input {
17-
color: #333;
18-
font-family: inherit;
19-
font-size: 0.95rem;
20-
width: 150px;
21-
border: 1px solid #dbdbdb;
22-
border-radius: 0.1em;
23-
line-height: 1.5;
24-
padding: 0 0.25em;
25-
}
26-
27-
@media screen and (min-width: 769px) {
28-
#search-input {
29-
width: 200px;
30-
}
31-
}
32-
331
.search-result-dropdown-menu {
342
position: absolute;
353
z-index: 100;
@@ -50,11 +18,6 @@
5018
}
5119

5220
@media screen and (max-width: 768px) {
53-
.navbar-brand .navbar-item + .navbar-item {
54-
padding-left: 0;
55-
padding-right: 0;
56-
}
57-
5821
.search-result-dropdown-menu {
5922
min-width: calc(100vw - 3.75rem);
6023
}
@@ -66,50 +29,47 @@
6629
background: #fff;
6730
border-radius: 4px;
6831
overflow: auto;
69-
padding: 0 8px 8px;
32+
padding: 0 8px;
7033
max-height: calc(100vh - 5.25rem);
71-
color: #333;
72-
}
73-
74-
.search-result-highlight {
75-
color: #174d8c;
76-
background: rgba(143, 187, 237, 0.1);
77-
padding: .1em .05em;
34+
line-height: 1.5;
7835
}
7936

8037
.search-result-item {
8138
display: flex;
82-
font-size: 1rem;
83-
margin-bottom: 0.5rem;
84-
margin-top: 0.5rem;
39+
margin: 0.5rem 0;
8540
}
8641

8742
.search-result-document-title {
8843
width: 33%;
8944
border-right: 1px solid #ddd;
90-
color: #a4a7ae;
45+
color: #02060c;
46+
font-weight: 500;
9147
font-size: 0.8rem;
92-
padding: 0.25rem 0.5rem 0.25rem 0;
48+
padding: 0.5rem 0.5rem 0.5rem 0;
9349
text-align: right;
9450
position: relative;
9551
word-wrap: break-word;
9652
}
9753

9854
.search-result-document-hit {
9955
flex: 1;
100-
font-size: 0.75em;
101-
color: #02060c;
102-
font-weight: 700;
56+
font-size: 0.75rem;
57+
color: #63676d;
10358
}
10459

10560
.search-result-document-hit > a {
10661
color: inherit;
10762
display: block;
108-
padding: 0.5rem 0 0.5rem 1rem;
109-
margin-bottom: 0.25rem;
63+
padding: 0.55rem 0.25rem 0.55rem 0.75rem;
11064
}
11165

11266
.search-result-document-hit > a:hover {
11367
background-color: rgba(69, 142, 225, 0.05);
11468
}
11569

70+
.search-result-highlight {
71+
color: #174d8c;
72+
background: rgba(143, 187, 237, 0.1);
73+
padding: 0.1em 0.05em;
74+
font-weight: 500;
75+
}

‎supplemental_ui/js/vendor/search.js ‎supplemental_ui/js/search-ui.js

+80-44
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
/* eslint-env browser */
2-
window.antoraLunr = (function (lunr) {
1+
/* global CustomEvent */
2+
;(function (globalScope) {
3+
/* eslint-disable no-var */
4+
var config = document.getElementById('search-ui-script').dataset
5+
var snippetLength = parseInt(config.snippetLength || 100, 10)
6+
var siteRootPath = config.siteRootPath || ''
7+
appendStylesheet(config.stylesheet)
38
var searchInput = document.getElementById('search-input')
49
var searchResult = document.createElement('div')
510
searchResult.classList.add('search-result-dropdown-menu')
611
searchInput.parentNode.appendChild(searchResult)
712

13+
function appendStylesheet (href) {
14+
if (!href) return
15+
document.head.appendChild(Object.assign(document.createElement('link'), { rel: 'stylesheet', href: href }))
16+
}
17+
818
function highlightText (doc, position) {
919
var hits = []
1020
var start = position[0]
@@ -17,9 +27,8 @@ window.antoraLunr = (function (lunr) {
1727

1828
var end = start + length
1929
var textEnd = text.length - 1
20-
var contextOffset = 15
21-
var contextAfter = end + contextOffset > textEnd ? textEnd : end + contextOffset
22-
var contextBefore = start - contextOffset < 0 ? 0 : start - contextOffset
30+
var contextAfter = end + snippetLength > textEnd ? textEnd : end + snippetLength
31+
var contextBefore = start - snippetLength < 0 ? 0 : start - snippetLength
2332
if (start === 0 && end === textEnd) {
2433
hits.push(highlightSpan)
2534
} else if (start === 0) {
@@ -36,18 +45,16 @@ window.antoraLunr = (function (lunr) {
3645
return hits
3746
}
3847

39-
function highlightTitle (hash, doc, position) {
48+
function highlightTitle (sectionTitle, doc, position) {
4049
var hits = []
4150
var start = position[0]
4251
var length = position[1]
4352

4453
var highlightSpan = document.createElement('span')
4554
highlightSpan.classList.add('search-result-highlight')
4655
var title
47-
if (hash) {
48-
title = doc.titles.filter(function (item) {
49-
return item.id === hash
50-
})[0].text
56+
if (sectionTitle) {
57+
title = sectionTitle.text
5158
} else {
5259
title = doc.title
5360
}
@@ -71,7 +78,7 @@ window.antoraLunr = (function (lunr) {
7178
return hits
7279
}
7380

74-
function highlightHit (metadata, hash, doc) {
81+
function highlightHit (metadata, sectionTitle, doc) {
7582
var hits = []
7683
for (var token in metadata) {
7784
var fields = metadata[token]
@@ -80,7 +87,7 @@ window.antoraLunr = (function (lunr) {
8087
if (positions.position) {
8188
var position = positions.position[0] // only higlight the first match
8289
if (field === 'title') {
83-
hits = highlightTitle(hash, doc, position)
90+
hits = highlightTitle(sectionTitle, doc, position)
8491
} else if (field === 'text') {
8592
hits = highlightText(doc, position)
8693
}
@@ -90,30 +97,32 @@ window.antoraLunr = (function (lunr) {
9097
return hits
9198
}
9299

93-
function createSearchResult(result, store, searchResultDataset) {
100+
function createSearchResult (result, store, searchResultDataset) {
94101
result.forEach(function (item) {
95-
var url = item.ref
96-
var hash
97-
if (url.includes('#')) {
98-
hash = url.substring(url.indexOf('#') + 1)
99-
url = url.replace('#' + hash, '')
102+
var ids = item.ref.split('-')
103+
var docId = ids[0]
104+
var doc = store[docId]
105+
var sectionTitle
106+
if (ids.length > 1) {
107+
var titleId = ids[1]
108+
sectionTitle = doc.titles.filter(function (item) {
109+
return String(item.id) === titleId
110+
})[0]
100111
}
101-
var doc = store[url]
102112
var metadata = item.matchData.metadata
103-
var hits = highlightHit(metadata, hash, doc)
104-
searchResultDataset.appendChild(createSearchResultItem(doc, item, hits))
113+
var hits = highlightHit(metadata, sectionTitle, doc)
114+
searchResultDataset.appendChild(createSearchResultItem(doc, sectionTitle, item, hits))
105115
})
106116
}
107117

108-
function createSearchResultItem (doc, item, hits) {
118+
function createSearchResultItem (doc, sectionTitle, item, hits) {
109119
var documentTitle = document.createElement('div')
110120
documentTitle.classList.add('search-result-document-title')
111121
documentTitle.innerText = doc.title
112122
var documentHit = document.createElement('div')
113123
documentHit.classList.add('search-result-document-hit')
114124
var documentHitLink = document.createElement('a')
115-
var rootPath = window.antora.basePath
116-
documentHitLink.href = rootPath + item.ref
125+
documentHitLink.href = siteRootPath + doc.url + (sectionTitle ? '#' + sectionTitle.hash : '')
117126
documentHit.appendChild(documentHitLink)
118127
hits.forEach(function (hit) {
119128
documentHitLink.appendChild(hit)
@@ -140,6 +149,11 @@ window.antoraLunr = (function (lunr) {
140149
return searchResultItem
141150
}
142151

152+
function clearSearchResults (reset) {
153+
if (reset === true) searchInput.value = ''
154+
searchResult.innerHTML = ''
155+
}
156+
143157
function search (index, text) {
144158
// execute an exact match search
145159
var result = index.search(text)
@@ -157,10 +171,7 @@ window.antoraLunr = (function (lunr) {
157171
}
158172

159173
function searchIndex (index, store, text) {
160-
// reset search result
161-
while (searchResult.firstChild) {
162-
searchResult.removeChild(searchResult.firstChild)
163-
}
174+
clearSearchResults(false)
164175
if (text.trim() === '') {
165176
return
166177
}
@@ -175,6 +186,10 @@ window.antoraLunr = (function (lunr) {
175186
}
176187
}
177188

189+
function confineEvent (e) {
190+
e.stopPropagation()
191+
}
192+
178193
function debounce (func, wait, immediate) {
179194
var timeout
180195
return function () {
@@ -191,22 +206,43 @@ window.antoraLunr = (function (lunr) {
191206
}
192207
}
193208

194-
function init (data) {
195-
var index = Object.assign({index: lunr.Index.load(data.index), store: data.store})
196-
var search = debounce(function () {
197-
searchIndex(index.index, index.store, searchInput.value)
198-
}, 100)
199-
searchInput.addEventListener('keydown', search)
200-
201-
// this is prevented in case of mousedown attached to SearchResultItem
202-
searchInput.addEventListener('blur', function (e) {
203-
while (searchResult.firstChild) {
204-
searchResult.removeChild(searchResult.firstChild)
205-
}
206-
})
209+
function enableSearchInput (enabled) {
210+
searchInput.disabled = !enabled
211+
searchInput.title = enabled ? '' : 'Loading index...'
207212
}
208213

209-
return {
210-
init: init,
214+
function initSearch (lunr, data) {
215+
var start = performance.now()
216+
var index = Object.assign({ index: lunr.Index.load(data.index), store: data.store })
217+
enableSearchInput(true)
218+
searchInput.dispatchEvent(
219+
new CustomEvent('loadedindex', {
220+
detail: {
221+
took: performance.now() - start,
222+
},
223+
})
224+
)
225+
var debug = 'URLSearchParams' in globalScope && new URLSearchParams(globalScope.location.search).has('lunr-debug')
226+
searchInput.addEventListener(
227+
'keydown',
228+
debounce(function (e) {
229+
if (e.key === 'Escape' || e.key === 'Esc') return clearSearchResults(true)
230+
try {
231+
var query = searchInput.value
232+
if (!query) return clearSearchResults()
233+
searchIndex(index.index, index.store, searchInput.value)
234+
} catch (err) {
235+
if (debug) console.debug('Invalid search query: ' + query + ' (' + err.message + ')')
236+
}
237+
}, 100)
238+
)
239+
searchInput.addEventListener('click', confineEvent)
240+
searchResult.addEventListener('click', confineEvent)
241+
document.documentElement.addEventListener('click', clearSearchResults)
211242
}
212-
})(window.lunr)
243+
244+
// disable the search input until the index is loaded
245+
enableSearchInput(false)
246+
247+
globalScope.initSearch = initSearch
248+
})(typeof globalThis !== 'undefined' ? globalThis : window)

0 commit comments

Comments
 (0)
Please sign in to comment.