Skip to content

Commit de1dcfd

Browse files
committed
write icon defs to shared JavaScript file
1 parent 4f86d27 commit de1dcfd

File tree

5 files changed

+104
-90
lines changed

5 files changed

+104
-90
lines changed

gulp.d/tasks/build-preview-pages.js

Lines changed: 98 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -39,73 +39,83 @@ module.exports = (src, previewSrc, previewDest, sink = () => map()) => (done) =>
3939
),
4040
])
4141
.then(([baseUiModel, { layouts }]) => [{ ...baseUiModel, env: process.env }, layouts])
42-
.then(([baseUiModel, layouts]) =>
42+
.then(([baseUiModel, layouts, iconDefs = new Map()]) =>
4343
vfs
4444
.src('**/*.adoc', { base: previewSrc, cwd: previewSrc })
4545
.pipe(
46-
map((file, enc, next) => {
47-
const siteRootPath = path.relative(ospath.dirname(file.path), ospath.resolve(previewSrc))
48-
const uiModel = { ...baseUiModel }
49-
uiModel.siteRootPath = siteRootPath
50-
uiModel.siteRootUrl = path.join(siteRootPath, 'index.html')
51-
uiModel.uiRootPath = path.join(siteRootPath, '_')
52-
if (file.stem === '404') {
53-
uiModel.page = { layout: '404', title: 'Page Not Found' }
54-
} else {
55-
const pageModel = (uiModel.page = { ...uiModel.page })
56-
const doc = asciidoctor.load(file.contents, { safe: 'safe', attributes: ASCIIDOC_ATTRIBUTES })
57-
const attributes = doc.getAttributes()
58-
pageModel.layout = doc.getAttribute('page-layout', 'default')
59-
pageModel.title = doc.getDocumentTitle()
60-
pageModel.url = '/' + file.relative.slice(0, -5) + '.html'
61-
if (file.stem === 'home') pageModel.home = true
62-
const componentName = doc.getAttribute('page-component-name', pageModel.src.component)
63-
const versionString = doc.getAttribute(
64-
'page-version',
65-
doc.hasAttribute('page-component-name') ? undefined : pageModel.src.version
66-
)
67-
let component
68-
let componentVersion
69-
if (componentName) {
70-
component = pageModel.component = uiModel.site.components[componentName]
71-
componentVersion = pageModel.componentVersion = versionString
72-
? component.versions.find(({ version }) => version === versionString)
73-
: component.latest
46+
map(
47+
(file, enc, next) => {
48+
const siteRootPath = path.relative(ospath.dirname(file.path), ospath.resolve(previewSrc))
49+
const uiModel = {
50+
...baseUiModel,
51+
preview: true,
52+
siteRootPath,
53+
siteRootUrl: path.join(siteRootPath, 'index.html'),
54+
uiRootPath: path.join(siteRootPath, '_'),
55+
}
56+
if (file.stem === '404') {
57+
uiModel.page = { layout: '404', title: 'Page Not Found' }
7458
} else {
75-
component = pageModel.component = Object.values(uiModel.site.components)[0]
76-
componentVersion = pageModel.componentVersion = component.latest
59+
const pageModel = (uiModel.page = { ...uiModel.page })
60+
const doc = asciidoctor.load(file.contents, { safe: 'safe', attributes: ASCIIDOC_ATTRIBUTES })
61+
const attributes = doc.getAttributes()
62+
pageModel.layout = doc.getAttribute('page-layout', 'default')
63+
pageModel.title = doc.getDocumentTitle()
64+
pageModel.url = '/' + file.relative.slice(0, -5) + '.html'
65+
if (file.stem === 'home') pageModel.home = true
66+
const componentName = doc.getAttribute('page-component-name', pageModel.src.component)
67+
const versionString = doc.getAttribute(
68+
'page-version',
69+
doc.hasAttribute('page-component-name') ? undefined : pageModel.src.version
70+
)
71+
let component
72+
let componentVersion
73+
if (componentName) {
74+
component = pageModel.component = uiModel.site.components[componentName]
75+
componentVersion = pageModel.componentVersion = versionString
76+
? component.versions.find(({ version }) => version === versionString)
77+
: component.latest
78+
} else {
79+
component = pageModel.component = Object.values(uiModel.site.components)[0]
80+
componentVersion = pageModel.componentVersion = component.latest
81+
}
82+
pageModel.module = 'ROOT'
83+
pageModel.relativeSrcPath = file.relative
84+
pageModel.version = componentVersion.version
85+
pageModel.displayVersion = componentVersion.displayVersion
86+
pageModel.editUrl = pageModel.origin.editUrlPattern.replace('%s', file.relative)
87+
pageModel.navigation = componentVersion.navigation || []
88+
pageModel.breadcrumbs = findNavPath(pageModel.url, pageModel.navigation)
89+
if (pageModel.component.versions.length > 1) {
90+
pageModel.versions = pageModel.component.versions.map(
91+
({ version, displayVersion, url }, idx, arr) => {
92+
const pageVersion = { version, displayVersion: displayVersion || version, url }
93+
if (version === component.latest.version) pageVersion.latest = true
94+
if (idx === arr.length - 1) {
95+
delete pageVersion.url
96+
pageVersion.missing = true
97+
}
98+
return pageVersion
99+
}
100+
)
101+
}
102+
pageModel.attributes = Object.entries({ ...attributes, ...componentVersion.asciidoc.attributes })
103+
.filter(([name, val]) => name.startsWith('page-'))
104+
.reduce((accum, [name, val]) => ({ ...accum, [name.substr(5)]: val }), {})
105+
pageModel.contents = Buffer.from(doc.convert())
77106
}
78-
pageModel.module = 'ROOT'
79-
pageModel.relativeSrcPath = file.relative
80-
pageModel.version = componentVersion.version
81-
pageModel.displayVersion = componentVersion.displayVersion
82-
pageModel.editUrl = pageModel.origin.editUrlPattern.replace('%s', file.relative)
83-
pageModel.navigation = componentVersion.navigation || []
84-
pageModel.breadcrumbs = findNavPath(pageModel.url, pageModel.navigation)
85-
if (pageModel.component.versions.length > 1) {
86-
pageModel.versions = pageModel.component.versions.map(({ version, displayVersion, url }, idx, arr) => {
87-
const pageVersion = { version, displayVersion: displayVersion || version, url }
88-
if (version === component.latest.version) pageVersion.latest = true
89-
if (idx === arr.length - 1) {
90-
delete pageVersion.url
91-
pageVersion.missing = true
92-
}
93-
return pageVersion
94-
})
107+
file.extname = '.html'
108+
try {
109+
file.contents = Buffer.from(layouts.get(uiModel.page.layout)(uiModel))
110+
registerIconDefs(iconDefs, file)
111+
next(null, file)
112+
} catch (e) {
113+
next(transformHandlebarsError(e, uiModel.page.layout))
95114
}
96-
pageModel.attributes = Object.entries({ ...attributes, ...componentVersion.asciidoc.attributes })
97-
.filter(([name, val]) => name.startsWith('page-'))
98-
.reduce((accum, [name, val]) => ({ ...accum, [name.substr(5)]: val }), {})
99-
pageModel.contents = Buffer.from(doc.convert())
100-
}
101-
file.extname = '.html'
102-
try {
103-
file.contents = Buffer.from(layouts.get(uiModel.page.layout)(uiModel))
104-
next(null, injectIconDefs(file))
105-
} catch (e) {
106-
next(transformHandlebarsError(e, uiModel.page.layout))
107-
}
108-
})
115+
},
116+
// NOTE parallel build overwrites default fontawesome-icon-defs.js, so we must use an alternate path
117+
() => writeIconDefs(iconDefs, ospath.join(previewDest, 'fontawesome-icon-defs.js'))
118+
)
109119
)
110120
.pipe(vfs.dest(previewDest))
111121
.on('error', (e) => done)
@@ -201,34 +211,35 @@ function findNavPath (currentUrl, node = [], current_path = [], root = true) {
201211
if (root) return []
202212
}
203213

204-
function injectIconDefs (file) {
214+
function registerIconDefs (iconDefs, file) {
205215
const contents = file.contents
206-
if (!contents.includes('<i class="fa')) return file
216+
if (!contents.includes('<i class="fa')) return
207217
const stringContents = contents.toString()
208-
const iconNames = stringContents.match(new RegExp('<i class="fa[brs]? fa-[^" ]+', 'g')).map((it) => {
209-
return it.substr(10).replace(' fa-', ' ')
210-
})
211-
if (!iconNames.length) return file
212-
const iconDefs = [...new Set(iconNames)].reduce((accum, it) => {
213-
const [iconPrefix, iconName] = it.split(' ').slice(0, 2)
214-
let iconDef = (iconPacks[iconPrefix] || {})['fa' + camelCase(iconName)]
215-
if (iconDef) {
216-
return accum.concat({ ...iconDef, prefix: iconPrefix })
217-
} else if (iconPrefix === 'fa') {
218-
const [realIconPrefix, realIconName] = iconShims[iconName] || []
219-
if (realIconName && (iconDef = (iconPacks[realIconPrefix] || {})['fa' + camelCase(realIconName)])) {
220-
return accum.concat({ ...iconDef, prefix: realIconPrefix })
218+
const iconNames = stringContents.match(/<i class="fa[brs]? fa-[^" ]+/g).map((it) => it.substr(10).replace('fa-', ''))
219+
if (!iconNames.length) return
220+
;[...new Set(iconNames)].reduce((accum, iconKey) => {
221+
if (!accum.has(iconKey)) {
222+
const [iconPrefix, iconName] = iconKey.split(' ').slice(0, 2)
223+
let iconDef = (iconPacks[iconPrefix] || {})['fa' + camelCase(iconName)]
224+
if (iconDef) {
225+
return accum.set(iconKey, { ...iconDef, prefix: iconPrefix })
226+
} else if (iconPrefix === 'fa') {
227+
const [realIconPrefix, realIconName] = iconShims[iconName] || []
228+
if (
229+
!accum.has((iconKey = `${realIconPrefix} ${realIconName}`)) &&
230+
realIconName &&
231+
(iconDef = (iconPacks[realIconPrefix] || {})['fa' + camelCase(realIconName)])
232+
) {
233+
return accum.set(iconKey, { ...iconDef, prefix: realIconPrefix })
234+
}
221235
}
222236
}
223237
return accum
224-
}, [])
225-
const iconData = `<script>\nwindow.FontAwesomeIconDefs = ${JSON.stringify(iconDefs)}\n</script>`
226-
file.contents = Buffer.from(
227-
stringContents.replace(new RegExp('<script async src="[^"]*_/js/vendor/fontawesome.js"></script>'), function (m) {
228-
return [iconData, m].join('\n')
229-
})
230-
)
231-
return file
238+
}, iconDefs)
239+
}
240+
241+
async function writeIconDefs (iconDefs, to) {
242+
return fs.writeFile(to, `window.FontAwesomeIconDefs = ${JSON.stringify([...iconDefs.values()])}\n`, 'utf8')
232243
}
233244

234245
function relativize (url) {
@@ -261,5 +272,7 @@ function toPromise (stream) {
261272
}
262273

263274
function camelCase (str) {
264-
return str.replace(/(?:^|-)(.)/g, function (_, l) { return l.toUpperCase() })
275+
return str.replace(/(?:^|-)(.)/g, function (_, l) {
276+
return l.toUpperCase()
277+
})
265278
}

preview-src/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
[abstract]
1313
From the *Data Bucket* menu, information and statistics about buckets and nodes is displayed for the entire Couchbase Server cluster.
14-
The information is aggregated from all the server nodes within the configured cluster for the selected bucket.
14+
The information is aggregated from all the icon:server[] nodes within the configured cluster for the selected bucket.
1515

1616
From the menu:Couchbase Web Console[Buckets] menu, click the menu:Statistics[] link for each bucket.
1717

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.FontAwesomeIconDefs = []
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
;(function () {
22
'use strict'
3-
43
;[].slice.call(document.querySelectorAll('td.icon>i.fa')).forEach(function (el) {
54
el.classList.remove('fa')
65
})
76

87
require('@fortawesome/fontawesome-free/js/v4-shims')
98
var fa = require('@fortawesome/fontawesome-svg-core')
109

11-
window.FontAwesomeIconDefs.forEach(function (faIconDef) {
10+
;(window.FontAwesomeIconDefs || []).forEach(function (faIconDef) {
1211
fa.library.add(faIconDef)
1312
})
1413

1514
fa.dom.i2svg()
1615
delete window.___FONT_AWESOME___
16+
delete window.FontAwesomeIconDefs
1717
})()

src/partials/footer-scripts.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{{#with page.attributes.content-scripts}}
33
{{{this}}}
44
{{/with}}
5+
<script async src="{{#unless preview}}{{{uiRootPath}}}/js/vendor/{{/unless}}fontawesome-icon-defs.js"></script>
6+
<script async src="{{{uiRootPath}}}/js/vendor/fontawesome.js"></script>
57
<script async src="{{{uiRootPath}}}/js/vendor/highlight.js"></script>
68
{{#if env.ALGOLIA_API_KEY}}
79
<script async id="search-script" src="{{{uiRootPath}}}/js/vendor/docsearch.js"
@@ -15,6 +17,4 @@
1517
{{!-- <script async id="feedback-script" src="{{{uiRootPath}}}/js/vendor/feedback.js?v=1" data-collector-id="de2e9313"></script> --}}
1618
<script async id="feedback-script" src="{{{uiRootPath}}}/js/vendor/feedback.js?v=1" data-collector-id="709818cb"></script>
1719
{{/if}}
18-
1920
<script async id="feedback-script" src="{{{uiRootPath}}}/js/vendor/feedback.js?v=1" data-collector-id="709818cb"></script>
20-
<script async src="{{{uiRootPath}}}/js/vendor/fontawesome.js"></script>

0 commit comments

Comments
 (0)