source: node_modules/refractor/lang/markup-templating.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.4 KB
Line 
1'use strict'
2
3module.exports = markupTemplating
4markupTemplating.displayName = 'markupTemplating'
5markupTemplating.aliases = []
6function markupTemplating(Prism) {
7 ;(function (Prism) {
8 /**
9 * Returns the placeholder for the given language id and index.
10 *
11 * @param {string} language
12 * @param {string|number} index
13 * @returns {string}
14 */
15 function getPlaceholder(language, index) {
16 return '___' + language.toUpperCase() + index + '___'
17 }
18 Object.defineProperties((Prism.languages['markup-templating'] = {}), {
19 buildPlaceholders: {
20 /**
21 * Tokenize all inline templating expressions matching `placeholderPattern`.
22 *
23 * If `replaceFilter` is provided, only matches of `placeholderPattern` for which `replaceFilter` returns
24 * `true` will be replaced.
25 *
26 * @param {object} env The environment of the `before-tokenize` hook.
27 * @param {string} language The language id.
28 * @param {RegExp} placeholderPattern The matches of this pattern will be replaced by placeholders.
29 * @param {(match: string) => boolean} [replaceFilter]
30 */
31 value: function (env, language, placeholderPattern, replaceFilter) {
32 if (env.language !== language) {
33 return
34 }
35 var tokenStack = (env.tokenStack = [])
36 env.code = env.code.replace(placeholderPattern, function (match) {
37 if (typeof replaceFilter === 'function' && !replaceFilter(match)) {
38 return match
39 }
40 var i = tokenStack.length
41 var placeholder // Check for existing strings
42 while (
43 env.code.indexOf((placeholder = getPlaceholder(language, i))) !==
44 -1
45 ) {
46 ++i
47 } // Create a sparse array
48 tokenStack[i] = match
49 return placeholder
50 }) // Switch the grammar to markup
51 env.grammar = Prism.languages.markup
52 }
53 },
54 tokenizePlaceholders: {
55 /**
56 * Replace placeholders with proper tokens after tokenizing.
57 *
58 * @param {object} env The environment of the `after-tokenize` hook.
59 * @param {string} language The language id.
60 */
61 value: function (env, language) {
62 if (env.language !== language || !env.tokenStack) {
63 return
64 } // Switch the grammar back
65 env.grammar = Prism.languages[language]
66 var j = 0
67 var keys = Object.keys(env.tokenStack)
68 function walkTokens(tokens) {
69 for (var i = 0; i < tokens.length; i++) {
70 // all placeholders are replaced already
71 if (j >= keys.length) {
72 break
73 }
74 var token = tokens[i]
75 if (
76 typeof token === 'string' ||
77 (token.content && typeof token.content === 'string')
78 ) {
79 var k = keys[j]
80 var t = env.tokenStack[k]
81 var s = typeof token === 'string' ? token : token.content
82 var placeholder = getPlaceholder(language, k)
83 var index = s.indexOf(placeholder)
84 if (index > -1) {
85 ++j
86 var before = s.substring(0, index)
87 var middle = new Prism.Token(
88 language,
89 Prism.tokenize(t, env.grammar),
90 'language-' + language,
91 t
92 )
93 var after = s.substring(index + placeholder.length)
94 var replacement = []
95 if (before) {
96 replacement.push.apply(replacement, walkTokens([before]))
97 }
98 replacement.push(middle)
99 if (after) {
100 replacement.push.apply(replacement, walkTokens([after]))
101 }
102 if (typeof token === 'string') {
103 tokens.splice.apply(tokens, [i, 1].concat(replacement))
104 } else {
105 token.content = replacement
106 }
107 }
108 } else if (
109 token.content
110 /* && typeof token.content !== 'string' */
111 ) {
112 walkTokens(token.content)
113 }
114 }
115 return tokens
116 }
117 walkTokens(env.tokens)
118 }
119 }
120 })
121 })(Prism)
122}
Note: See TracBrowser for help on using the repository browser.