Skip to content

Commit 8b8295b

Browse files
committed
integrate Font Awesome Pro 5 into UI preview
1 parent cbdcd07 commit 8b8295b

File tree

6 files changed

+121
-22
lines changed

6 files changed

+121
-22
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
const asciidoctor = require('asciidoctor.js')()
44
const fs = require('fs-extra')
55
const handlebars = require('handlebars')
6+
const iconPacks = {
7+
fas: (() => {
8+
try {
9+
return require('@fortawesome/pro-solid-svg-icons')
10+
} catch (e) {}
11+
})(),
12+
far: (() => {
13+
try {
14+
return require('@fortawesome/pro-regular-svg-icons')
15+
} catch (e) {}
16+
})(),
17+
fab: require('@fortawesome/free-brands-svg-icons'),
18+
}
19+
iconPacks.fa = iconPacks.fas
620
const { obj: map } = require('through2')
721
const merge = require('merge-stream')
822
const ospath = require('path')
@@ -83,7 +97,7 @@ module.exports = (src, previewSrc, previewDest, sink = () => map()) => (done) =>
8397
file.extname = '.html'
8498
try {
8599
file.contents = Buffer.from(layouts.get(uiModel.page.layout)(uiModel))
86-
next(null, file)
100+
next(null, injectIconDefs(file))
87101
} catch (e) {
88102
next(transformHandlebarsError(e, uiModel.page.layout))
89103
}
@@ -183,6 +197,30 @@ function findNavPath (currentUrl, node = [], current_path = [], root = true) {
183197
if (root) return []
184198
}
185199

200+
function injectIconDefs (file) {
201+
const contents = file.contents
202+
if (!contents.includes('<i class="fa')) return file
203+
const contentsString = contents.toString()
204+
const iconNames = contentsString.match(new RegExp('<i class="fa[brs]? fa-[^" ]+', 'g')).map((it) => {
205+
let [iconPrefix, iconName, ] = it.substr(10).split(' fa-')
206+
iconName = iconName.replace(/(?:^|-)(.)/g, function (_, l) { return l.toUpperCase() })
207+
return iconPrefix + '|' + iconName
208+
})
209+
if (!iconNames.length) return file
210+
const iconDefs = [...new Set(iconNames)].reduce((accum, it) => {
211+
const [iconPrefix, iconName, ] = it.split('|')
212+
const iconDef = iconPacks[iconPrefix] && iconPacks[iconPrefix]['fa' + iconName]
213+
return iconDef ? accum.concat({ ...iconDef, prefix: iconPrefix }) : accum
214+
}, [])
215+
const iconData = `<script>\nwindow.FontAwesomeIconDefs = ${JSON.stringify(iconDefs)}\n</script>`
216+
file.contents = Buffer.from(
217+
contentsString.replace(new RegExp('<script async src="[^"]*_/js/vendor/fontawesome.js"></script>'), function (m) {
218+
return [iconData, m].join('\n')
219+
})
220+
)
221+
return file
222+
}
223+
186224
function relativize (url) {
187225
return url ? (url.charAt() === '#' ? url : url.slice(1)) : '#'
188226
}

package-lock.json

Lines changed: 60 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
],
1616
"devDependencies": {
1717
"@octokit/rest": "~18.0",
18+
"@fortawesome/fontawesome-svg-core": "~1.2",
19+
"@fortawesome/free-brands-svg-icons": "~5.13",
1820
"asciidoctor.js": "1.5.9",
1921
"autoprefixer": "~9.8",
2022
"browser-pack-flat": "~3.4",
@@ -59,5 +61,9 @@
5961
"typeface-roboto-mono": "0.0.75",
6062
"vinyl-buffer": "~1.0",
6163
"vinyl-fs": "~3.0"
64+
},
65+
"optionalDependencies": {
66+
"@fortawesome/pro-solid-svg-icons": "~5.13",
67+
"@fortawesome/pro-regular-svg-icons": "~5.13"
6268
}
6369
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
;(function () {
2+
'use strict'
3+
4+
;[].slice.call(document.querySelectorAll('td.icon>i.fa')).forEach(function (el) {
5+
el.classList.remove('fa')
6+
})
7+
8+
var fa = require('@fortawesome/fontawesome-svg-core')
9+
10+
window.FontAwesomeIconDefs.forEach(function (faIconDef) {
11+
fa.library.add(faIconDef)
12+
})
13+
14+
fa.dom.i2svg()
15+
})()

src/partials/footer-scripts.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
{{/if}}
1818

1919
<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>

src/partials/head-last.hbs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<link rel="stylesheet" href="{{{uiRootPath}}}/css/site.css">
2-
{{!-- include fontAwesome CDN --}}
3-
<script src="https://kit.fontawesome.com/4a5569d39d.js" crossorigin="anonymous"></script>
42
<script src="{{{uiRootPath}}}/js/vendor/jquery.js"></script>
53
{{#with env.OPTANON_SCRIPT_URL}}
64
<script src="{{{@root.uiRootPath}}}/js/vendor/jquery.js"></script>

0 commit comments

Comments
 (0)