-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathshowdown-extensions.js
More file actions
99 lines (70 loc) · 3.23 KB
/
showdown-extensions.js
File metadata and controls
99 lines (70 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* eslint-disable prettier/prettier */
/* global Prism */
import showdown from 'showdown';
import config from 'ember-get-config';
let fieldGuideConfig = config['field-guide'] || {};
export function initialize(/* application */) {
showdown.subParser('githubCodeBlocks', function (text, options, globals) {
'use strict';
// early exit if option is not enabled
if (!options.ghCodeBlocks) {
return text;
}
text = globals.converter._dispatch('githubCodeBlocks.before', text, options, globals);
text += '¨0';
text = text.replace(/(?:^|\n)(?: {0,3})(```+|~~~+)(?: *)([^\s`~]*)\n([\s\S]*?)\n(?: {0,3})\1/g, function (wholeMatch, delim, languageBlock, inputCodeblock) {
var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n';
let codeblock = inputCodeblock;
// First parse the github code block
// codeblock = showdown.subParser('encodeCode')(codeblock, options, globals);
codeblock = showdown.subParser('detab')(codeblock, options, globals);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
let match = languageBlock.match(/(\w+)({(.*)})?/);
let language = '';
let attributeString = '';
if(match && match[1]) {
language = match[1];
// languageString = ' class="' + match[1] + ' language-' + match[1] + '"';
}
if (match && match[3]) {
attributeString = match[3];
}
if(!language) {
// set the default to shell
language = 'shell';
}
let highlightedCodeBlock = Prism.highlight(codeblock, Prism.languages[language], language) + end;
// escape { and } for the code sample
highlightedCodeBlock = highlightedCodeBlock.replace(/{/g, '{').replace(/}/g, '}')
let preBlock = `<pre class="language-${language}"><code ${language ? `class="${language} language-${language}"` : ''}>${highlightedCodeBlock}</code></pre>`;
let autoExecuteLanguages = fieldGuideConfig.autoExecuteLanguages || ['html', 'handlebars', 'hbs', 'glimmer'];
let selfExecutingBlock = `<div class="self-executing-code-block">
<div class="example">
${inputCodeblock}
</div>
${preBlock}
</div>`;
if(attributeString.includes('data-execute=false')) {
codeblock = preBlock;
} else if (attributeString.includes('data-execute=true')) {
codeblock = selfExecutingBlock;
} else if (autoExecuteLanguages.includes(language)) {
codeblock = selfExecutingBlock
} else {
codeblock = preBlock;
}
codeblock = showdown.subParser('hashBlock')(codeblock, options, globals);
// Since GHCodeblocks can be false positives, we need to
// store the primitive text and the parsed text in a global var,
// and then return a token
return '\n\n¨G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
});
// attacklab: strip sentinel
text = text.replace(/¨0/, '');
return globals.converter._dispatch('githubCodeBlocks.after', text, options, globals);
});
}
export default {
initialize
};