-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from megadoc/prismjs
Perform syntax-highlighting at compile-time
- Loading branch information
Showing
21 changed files
with
251 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ selection.json | |
# runtime junk | ||
npm-debug.log | ||
/examples/*/public | ||
/megadoc.sublime-workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var Prism = require('prismjs'); | ||
var assign = require('lodash').assign; | ||
|
||
function CodeRenderer(userConfig) { | ||
var config = assign({ | ||
languages: [], | ||
aliases: {} | ||
}, userConfig); | ||
|
||
config.languages.forEach(function(name) { | ||
try { | ||
require('prismjs/components/prism-' + name); | ||
} | ||
catch(e) { | ||
console.warn("SyntaxHighlighter: definition for language '%s' could not be found", | ||
name | ||
); | ||
} | ||
}); | ||
|
||
return function renderCode(code, language) { | ||
var languageCode = config.aliases[language] || language; | ||
var grammar = Prism.languages[languageCode]; | ||
var html = code; | ||
|
||
if (grammar) { | ||
html = Prism.highlight(code, grammar); | ||
} | ||
|
||
return ( | ||
'<pre class="language-' + (grammar ? languageCode : 'none') + '"><code>' + | ||
html + | ||
'</code></pre>' | ||
); | ||
}; | ||
} | ||
|
||
module.exports = CodeRenderer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var _ = require('lodash'); | ||
var RendererUtils = require('./RendererUtils'); | ||
var multiline = require('multiline-slash'); | ||
|
||
var AnchorableHeadingTmpl = _.template( | ||
multiline(function() {; | ||
// <h<%- level %> class="anchorable-heading"> | ||
// <a name="<%- id %>" class="anchorable-heading__anchor"></a> | ||
// <a href="#<%- id %>" class="anchorable-heading__link icon icon-link"></a><span class="anchorable-heading__text"><%= text %></span> | ||
// </h<%- level %>> | ||
}) | ||
); | ||
|
||
var HeadingTmpl = _.template( | ||
multiline(function() {; | ||
// <h<%- level %>> | ||
// <%= text %> | ||
// </h<%- level %>> | ||
}) | ||
); | ||
|
||
function renderHeading(text, level, state, runOptions) { | ||
var scopedId = RendererUtils.normalizeHeading( | ||
RendererUtils.htmlToText(text.split('\n')[0]) | ||
); | ||
|
||
var id = state.baseURL ? joinBySlash(state.baseURL, scopedId) : scopedId; | ||
|
||
// TODO: gief a markdown analyzer, really... | ||
state.toc.push({ | ||
id: id, | ||
scopedId: scopedId, | ||
level: level, | ||
html: text, | ||
text: RendererUtils.markdownToText(text).trim() | ||
}); | ||
|
||
if (runOptions.anchorableHeadings && id && id.length) { | ||
return AnchorableHeadingTmpl({ | ||
// we need to strip any leading # because in SinglePageMode all baseURL | ||
// values will have that | ||
id: stripLeadingHash(id), | ||
level: level, | ||
text: text, | ||
}); | ||
} | ||
else { | ||
return HeadingTmpl({ level: level, text: text }); | ||
} | ||
}; | ||
|
||
function stripLeadingHash(x) { | ||
return x[0] === '#' ? x.slice(1) : x; | ||
} | ||
|
||
function joinBySlash(a, b) { | ||
var shouldDelimit = a && a[a.length-1] !== '/'; | ||
|
||
return a + (shouldDelimit ? '/' : '') + b; | ||
} | ||
|
||
module.exports = renderHeading; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var escapeHTML = require('lodash').escape; | ||
var RE_INTERNAL_LINK = Object.freeze(/^mega:\/\//); | ||
|
||
function LinkRenderer(config) { | ||
return function renderLink(srcHref, title, text) { | ||
var href = srcHref.replace(RE_INTERNAL_LINK, ''); | ||
var tagString = '<a href="' + href + '"'; | ||
|
||
if (title) { | ||
tagString += ' title="' + escapeHTML(title) + '"'; | ||
} | ||
|
||
if (href === srcHref && href[0] !== '#' && config.launchExternalLinksInNewTabs) { | ||
tagString += ' target="_blank"'; | ||
} | ||
|
||
return tagString + '>' + text + '</a>'; | ||
}; | ||
} | ||
|
||
module.exports = LinkRenderer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.