|
| 1 | + |
| 2 | +const UNESCAPE_RE = /(?<markdown>\$(?<model_type>[a-z_]+)-(?<model_id>\d+))/g |
| 3 | + |
| 4 | +function model_link (state) { |
| 5 | + /** |
| 6 | + * Render Model tag links |
| 7 | + * i.e. $model_name-<model_id> |
| 8 | + */ |
| 9 | + |
| 10 | + const max = state.posMax |
| 11 | + let start = state.pos |
| 12 | + let end = state.pos |
| 13 | + |
| 14 | + const re = new RegExp(UNESCAPE_RE); |
| 15 | + |
| 16 | + const fields = [ ...String(state.src).matchAll(re) ] |
| 17 | + |
| 18 | + |
| 19 | + if( fields.length === 0 ) { |
| 20 | + state.pos = start + 1 |
| 21 | + return false |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + for( let item_link of fields) { |
| 26 | + |
| 27 | + start = item_link.index |
| 28 | + end = start + String(item_link.groups.markdown).length |
| 29 | + |
| 30 | + if( state.tokens.length === 0 && start >= 0 ) { // start text |
| 31 | + |
| 32 | + const before_text = state.push('text', '', 0) |
| 33 | + before_text.content = state.src.slice(0, start) |
| 34 | + |
| 35 | + }else if ( start !== state.posMax ) { // middle text |
| 36 | + |
| 37 | + const middle_text = state.push('text', '', 0) |
| 38 | + middle_text.content = state.src.slice(state.posMax, start) |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + state.pos = start |
| 43 | + state.posMax = end |
| 44 | + |
| 45 | + if( state.env.models[item_link.groups.model_type][item_link.groups.model_id] ?? null ) { |
| 46 | + |
| 47 | + const span_o = state.push('span_open', 'span', 1) |
| 48 | + |
| 49 | + const anchor_o = state.push('a_open', 'a', 1) |
| 50 | + anchor_o.attrPush(['href', state.env.models[item_link.groups.model_type][item_link.groups.model_id].url]) |
| 51 | + |
| 52 | + |
| 53 | + // const icon_o = state.push('icon_open', 'span', 1) |
| 54 | + |
| 55 | + // const icon_t = state.push('text', '', 0) |
| 56 | + // icon_t.content = 'icon ' |
| 57 | + |
| 58 | + // const icon_c = state.push('icon_close', 'span', -1) |
| 59 | + |
| 60 | + |
| 61 | + const anchor_t = state.push('text', '', 0) |
| 62 | + anchor_t.content = state.env.models[item_link.groups.model_type][item_link.groups.model_id].title |
| 63 | + |
| 64 | + |
| 65 | + const item_o = state.push('item_open', 'span', 1) |
| 66 | + item_o.attrPush(["class", "sub-script metadata"]) |
| 67 | + |
| 68 | + const item_t = state.push('text', '', 0) |
| 69 | + item_t.content = ', ' + String( item_link.groups.model_type ) + ' ' |
| 70 | + |
| 71 | + const item_c = state.push('item_open', 'span', -1) |
| 72 | + |
| 73 | + |
| 74 | + const anchor_c = state.push('a_close', 'a', -1) |
| 75 | + |
| 76 | + |
| 77 | + const span_c = state.push('span_close', 'span', -1) |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + state.pos = end |
| 86 | + state.posMax = max |
| 87 | + |
| 88 | + |
| 89 | + return true |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +export default function model_link_plugin (md) { |
| 94 | + md.inline.ruler.after('emphasis', 'sup', model_link) |
| 95 | +}; |
0 commit comments