Skip to content

Commit

Permalink
Add JavaScript syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
ozan committed Dec 22, 2015
1 parent e88e44e commit 40fed38
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 0 additions & 1 deletion book/language-switching.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// TODO:
// retain latest in stack-like structure
// improve select visuals
// highlight javascript too
// litjs

const languageName = {
Expand Down
13 changes: 11 additions & 2 deletions prism-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const prism = require('prismjs')

const pythonCode = /```python\n([\s\S]*?)```/g

const javascriptCode = /```javascript\n([\s\S]*?)```/g

const pythonGrammar = {
'triple-quoted-string': {
pattern: /"""[\s\S]+?"""|'''[\s\S]+?'''/,
Expand All @@ -29,15 +31,22 @@ const pythonGrammar = {
punctuation: /[{}[\];(),.:]/,
}

const highlight = (match, group) => `
const highlightPython = (match, group) => `
<pre><code class="language-python">${prism.highlight(group, pythonGrammar)}</code></pre>
`

const highlightJavaScript = (match, group) => `
<pre><code class="language-javascript">${prism.highlight(group, prism.languages.javascript)}</code></pre>
`

const highlightCode = files => {
for (let path in files) {
if (path.search('\.md$') !== -1) {
const file = files[path]
const replaced = file.contents.toString('utf8').replace(pythonCode, highlight)
const replaced = file.contents
.toString('utf8')
.replace(pythonCode, highlightPython)
.replace(javascriptCode, highlightJavaScript)
file.contents = new Buffer(replaced, 'utf8')
}
}
Expand Down

0 comments on commit 40fed38

Please sign in to comment.