Skip to content

Commit

Permalink
fix: load the dependencies for prism languages
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 18, 2018
1 parent ae16c04 commit 9ed2c4f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 6 deletions.
19 changes: 19 additions & 0 deletions build/generate-required-languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs')

let {languages} = require('prismjs/components')

languages = Object.keys(languages).reduce((res, name) => {
if (languages[name].require) {
res[name] = languages[name].require
}
return res
}, {})
languages.builtin = Object.keys(require('prismjs').languages).filter(lang => {
return !['extend', 'insertBefore', 'DFS'].includes(lang)
})

fs.writeFileSync(
'./src/utils/prismLanguages.json',
JSON.stringify(languages, null, 2),
'utf8'
)
3 changes: 2 additions & 1 deletion build/poi.lib.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
sourceMap: false,
cleanOutDir: false,
constants: {
__DOCUTE_VERSION__: JSON.stringify(pkg.version)
__DOCUTE_VERSION__: JSON.stringify(pkg.version),
__PRISM_VERSION__: JSON.stringify(require('prismjs/package').version)
}
}
3 changes: 2 additions & 1 deletion build/poi.website.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = {
title: 'Docute'
},
constants: {
__DOCUTE_VERSION__: JSON.stringify(pkg.version)
__DOCUTE_VERSION__: JSON.stringify(pkg.version),
__PRISM_VERSION__: JSON.stringify(require('prismjs/package').version)
},
plugins: [
{
Expand Down
27 changes: 23 additions & 4 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* globals __PRISM_VERSION__ */
import Vue from 'vue'
import Vuex from 'vuex'
import fetch from 'unfetch'
Expand All @@ -7,6 +8,7 @@ import {getFilenameByPath, isExternalLink} from './utils'
import markedRenderer from './utils/markedRenderer'
import hooks from './hooks'
import load from './utils/load'
import prismLanguages from './utils/prismLanguages'

Vue.use(Vuex)

Expand Down Expand Up @@ -82,14 +84,31 @@ const store = new Vuex.Store({
},

fetchPrismLanguages({getters}) {
if (!getters.config.highlight) {
const langs = getters.config.highlight

if (!langs || langs.length === 0) {
return Promise.resolve()
}

return load(
getters.config.highlight.map(lang => {
return `https://unpkg.com/prismjs/components/prism-${lang}.min.js`
}),
langs
.reduce((res, lang) => {
if (prismLanguages[lang]) {
res = res.concat(prismLanguages[lang])
}
res.push(lang)
return res
}, [])
.filter((lang, i, arr) => {
// Dedupe
return (
arr.indexOf(lang) === i &&
prismLanguages.builtin.indexOf(lang) === -1
)
})
.map(lang => {
return `https://unpkg.com/prismjs@${__PRISM_VERSION__}/components/prism-${lang}.js`
}),
'prism-languages'
)
}
Expand Down
92 changes: 92 additions & 0 deletions src/utils/prismLanguages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"javascript": "clike",
"actionscript": "javascript",
"arduino": "cpp",
"aspnet": [
"markup",
"csharp"
],
"bison": "c",
"c": "clike",
"csharp": "clike",
"cpp": "c",
"coffeescript": "javascript",
"crystal": "ruby",
"css-extras": "css",
"d": "clike",
"dart": "clike",
"django": "markup",
"erb": [
"ruby",
"markup-templating"
],
"fsharp": "clike",
"flow": "javascript",
"glsl": "clike",
"go": "clike",
"groovy": "clike",
"haml": "ruby",
"handlebars": "markup-templating",
"haxe": "clike",
"java": "clike",
"jolie": "clike",
"kotlin": "clike",
"less": "css",
"markdown": "markup",
"markup-templating": "markup",
"n4js": "javascript",
"nginx": "clike",
"objectivec": "c",
"opencl": "cpp",
"parser": "markup",
"php": [
"clike",
"markup-templating"
],
"php-extras": "php",
"plsql": "sql",
"processing": "clike",
"protobuf": "clike",
"pug": "javascript",
"qore": "clike",
"jsx": [
"markup",
"javascript"
],
"tsx": [
"jsx",
"typescript"
],
"reason": "clike",
"ruby": "clike",
"sass": "css",
"scss": "css",
"scala": "java",
"smarty": "markup-templating",
"soy": "markup-templating",
"swift": "clike",
"tap": "yaml",
"textile": "markup",
"tt2": [
"clike",
"markup-templating"
],
"twig": "markup",
"typescript": "javascript",
"vbnet": "basic",
"velocity": "markup",
"wiki": "markup",
"xeora": "markup",
"xquery": "markup",
"builtin": [
"markup",
"xml",
"html",
"mathml",
"svg",
"css",
"clike",
"javascript",
"js"
]
}

0 comments on commit 9ed2c4f

Please sign in to comment.