|
| 1 | +const OPEN_IDENTIFIER = '_open'; |
| 2 | +const CLOSE_IDENTIFIER = '_close'; |
| 3 | +const INLINE_TYPE = 'inline'; |
| 4 | + |
| 5 | +const isOpenToken = ({ type }) => type.includes(OPEN_IDENTIFIER); |
| 6 | +const isCloseToken = ({ type }) => type.includes(CLOSE_IDENTIFIER); |
| 7 | +const isInlineToken = ({ type }) => type === INLINE_TYPE; |
| 8 | + |
| 9 | +export default class TokenTree { |
| 10 | + constructor(tokens = [], options, rOptions) { |
| 11 | + this.i = -1; |
| 12 | + this.tokens = tokens; |
| 13 | + this.options = options; |
| 14 | + this.rOptions = rOptions; |
| 15 | + |
| 16 | + return this.buildTokenTree(); |
| 17 | + } |
| 18 | + |
| 19 | + getType(token) { |
| 20 | + return typeof this.options.tokens[token.type] === 'function' |
| 21 | + ? this.options.tokens[token.type](token, this.rOptions) |
| 22 | + : this.options.tokens[token.type]; |
| 23 | + } |
| 24 | + |
| 25 | + buildToken(token, type = this.getType(token), children) { |
| 26 | + if (Array.isArray(type)) { |
| 27 | + return type.reduceRight((child, typee, index) => ( |
| 28 | + this.buildToken(token, typee, index < type.length - 1 ? [child] : undefined) |
| 29 | + ), null); |
| 30 | + } |
| 31 | + |
| 32 | + if (!type) { |
| 33 | + return token.content; |
| 34 | + } |
| 35 | + |
| 36 | + return ({ |
| 37 | + type, |
| 38 | + props: this.buildTokenProps(type, token), |
| 39 | + children: token.content, |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + buildTokenTree() { |
| 44 | + const ts = this.tokens; |
| 45 | + const collection = []; |
| 46 | + |
| 47 | + while (++this.i < ts.length) { |
| 48 | + if (isOpenToken(ts[this.i])) { |
| 49 | + collection.push( |
| 50 | + this.buildToken( |
| 51 | + ts[this.i], |
| 52 | + this.getType(ts[this.i]), |
| 53 | + this.buildTokenTree() |
| 54 | + ) |
| 55 | + ); |
| 56 | + } else if (isCloseToken(ts[this.i])) { |
| 57 | + return collection; |
| 58 | + } else if (isInlineToken(ts[this.i])) { |
| 59 | + new TokenTree(ts[this.i].children, this.options, this.rOptions) |
| 60 | + .forEach((token) => collection.push(token)); |
| 61 | + } else { |
| 62 | + collection.push(this.buildToken(ts[this.i])); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return collection; |
| 67 | + } |
| 68 | + |
| 69 | + buildTokenProps(type, token) { |
| 70 | + const props = {}; |
| 71 | + |
| 72 | + if (typeof this.options.components[type] === 'function') { |
| 73 | + props.options = this.rOptions; |
| 74 | + } |
| 75 | + |
| 76 | + [token, this.rOptions].forEach((prps) => { |
| 77 | + Object.keys(prps).forEach((prop) => { |
| 78 | + const propValue = this.resolveProp(type, prop, prps[prop]); |
| 79 | + if (propValue) props[propValue.key] = propValue.value; |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + return props; |
| 84 | + } |
| 85 | + |
| 86 | + resolveProp(type, prop, propValue) { |
| 87 | + const component = this.options.components[type]; |
| 88 | + const resolver = this.options.remarkableProps[prop]; |
| 89 | + |
| 90 | + if (!resolver) return; |
| 91 | + |
| 92 | + if (typeof resolver === 'function') { |
| 93 | + return this.resolveFunctionProp(type, resolver(propValue, type), prop, propValue); |
| 94 | + } |
| 95 | + |
| 96 | + if (typeof resolver === 'string') { |
| 97 | + return this.resolveStringProp(resolver, propValue); |
| 98 | + } |
| 99 | + |
| 100 | + if (resolver === true || typeof component === 'function') { |
| 101 | + return this.resolveTrueProp(prop, propValue); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + resolveFunctionProp(type, keyValue, prop, propValue) { |
| 106 | + if (keyValue) { |
| 107 | + return { |
| 108 | + key: keyValue.key || prop, |
| 109 | + value: keyValue.hasOwnProperty('value') |
| 110 | + ? keyValue.value |
| 111 | + : propValue, |
| 112 | + }; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + resolveStringProp(resolver, propValue) { |
| 117 | + return { |
| 118 | + key: resolver, |
| 119 | + value: propValue, |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + resolveTrueProp(prop, propValue) { |
| 124 | + return { |
| 125 | + key: prop, |
| 126 | + value: propValue, |
| 127 | + }; |
| 128 | + } |
| 129 | +} |
0 commit comments