|
| 1 | +/** |
| 2 | + * Copyright (c) Zeste de Savoir (https://zestedesavoir.com) |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, |
| 8 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following |
| 11 | + * conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * Patched version of https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-disable-tokenizers |
| 28 | + * to avoid newline issues described in https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-disable-tokenizers |
| 29 | + */ |
| 30 | + |
| 31 | +import clone from 'clone' |
| 32 | + |
| 33 | +const noop = () => false |
| 34 | + |
| 35 | +const throwing = (msg) => |
| 36 | + () => { |
| 37 | + throw new Error(msg) |
| 38 | + } |
| 39 | + |
| 40 | +function ignore({ block = [], inline = [] } = {}) { |
| 41 | + if (block.length) { |
| 42 | + block |
| 43 | + .filter((key) => { |
| 44 | + if (Array.isArray(key)) return block.map(xs => xs[0]).includes(key[0]) |
| 45 | + return block.includes(key) |
| 46 | + }) |
| 47 | + .forEach((key) => { |
| 48 | + if (Array.isArray(key) && key.length === 2) { |
| 49 | + this.Parser.prototype.blockTokenizers[key[0]] = throwing(key[1]) |
| 50 | + } else { |
| 51 | + this.Parser.prototype.blockTokenizers[key] = noop |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + if (inline.length) { |
| 57 | + inline |
| 58 | + .filter((key) => { |
| 59 | + if (Array.isArray(key)) return inline.map(xs => xs[0]).includes(key[0]) |
| 60 | + return inline.includes(key) |
| 61 | + }) |
| 62 | + .forEach((key) => { |
| 63 | + let tokenizerName |
| 64 | + let replacer |
| 65 | + if (Array.isArray(key) && key.length === 2) { |
| 66 | + tokenizerName = key[0] |
| 67 | + replacer = throwing(key[1]) |
| 68 | + } else { |
| 69 | + tokenizerName = key |
| 70 | + replacer = clone(noop) |
| 71 | + } |
| 72 | + if (this.Parser.prototype.inlineTokenizers[tokenizerName]) { |
| 73 | + Object |
| 74 | + .keys(this.Parser.prototype.inlineTokenizers[tokenizerName]) |
| 75 | + .forEach((prop) => { |
| 76 | + replacer[prop] = this.Parser.prototype.inlineTokenizers[tokenizerName][prop] |
| 77 | + }) |
| 78 | + } |
| 79 | + this.Parser.prototype.inlineTokenizers[tokenizerName] = replacer |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export default ignore |
0 commit comments