|
| 1 | +function install (hook, vm) { |
| 2 | + const codeContainer = { |
| 3 | + counter: 0, |
| 4 | + codes: {} |
| 5 | + }; |
| 6 | + |
| 7 | + hook.beforeEach(function (content) { |
| 8 | + codeContainer.counter = 0; |
| 9 | + codeContainer.codes = {}; |
| 10 | + return content; |
| 11 | + }); |
| 12 | + |
| 13 | + hook.mounted(function () { |
| 14 | + if (vm.compiler._marked.Renderer.prototype.code) { |
| 15 | + const original = vm.compiler._marked.defaults.renderer.code; |
| 16 | + |
| 17 | + const code = (code, infostring, escaped) => { |
| 18 | + const infostringParts = infostring.split(' '); |
| 19 | + let options; |
| 20 | + |
| 21 | + if (infostringParts.length > 1) { |
| 22 | + const [lang, ...rest] = infostringParts; |
| 23 | + infostring = lang; |
| 24 | + try { |
| 25 | + options = JSON.parse(rest.join(' ')); |
| 26 | + } catch (e) { |
| 27 | + console.error('invalid code options'); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const originalResult = original(code, infostring, escaped); |
| 32 | + |
| 33 | + if (typeof options === 'object') { |
| 34 | + let { lineNumbers, highlight } = options; |
| 35 | + |
| 36 | + if (typeof lineNumbers === 'boolean') { |
| 37 | + lineNumbers = { enabled: lineNumbers }; |
| 38 | + } else if (typeof lineNumbers === 'number') { |
| 39 | + lineNumbers = { enabled: true, start: lineNumbers }; |
| 40 | + } |
| 41 | + |
| 42 | + const additionalAttributes = []; |
| 43 | + if (typeof lineNumbers === 'object') { |
| 44 | + if (lineNumbers.enabled) { |
| 45 | + additionalAttributes.push(`class="language-${infostring} line-numbers"`); |
| 46 | + } |
| 47 | + if (lineNumbers.start) { |
| 48 | + additionalAttributes.push(`data-start="${lineNumbers.start}"`); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (typeof highlight === 'string') { |
| 53 | + highlight = { line: highlight }; |
| 54 | + } |
| 55 | + |
| 56 | + if (typeof highlight === 'object') { |
| 57 | + if (highlight.line) { |
| 58 | + additionalAttributes.push(`data-line="${highlight.line}"`); |
| 59 | + } |
| 60 | + if (highlight.lineOffset) { |
| 61 | + additionalAttributes.push(`data-line-offset="${highlight.lineOffset}"`); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (additionalAttributes.length) { |
| 66 | + codeContainer.codes[codeContainer.counter] = code; |
| 67 | + additionalAttributes.push(`data-code-counter="${codeContainer.counter}"`); |
| 68 | + codeContainer.counter += 1; |
| 69 | + |
| 70 | + return originalResult.replace(/^<pre /, `<pre ${additionalAttributes.join(' ')} `); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return originalResult; |
| 75 | + }; |
| 76 | + |
| 77 | + vm.compiler._marked.use({ renderer: { code } }); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + hook.doneEach(function () { |
| 82 | + document.querySelector('#main').querySelectorAll('pre[data-code-counter]').forEach(pre => { |
| 83 | + const env = { |
| 84 | + code: codeContainer.codes[pre.dataset.codeCounter], |
| 85 | + element: pre.querySelector(':scope > code') |
| 86 | + }; |
| 87 | + |
| 88 | + window.Prism.hooks.run('complete', env); |
| 89 | + }); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +window.$docsify.plugins = [].concat(install, window.$docsify.plugins); |
0 commit comments