Skip to content

Commit 7a9dd36

Browse files
committed
feat(function): Markdown-it plugin to render model tags
ref: #40 nofusscomputing/centurion_erp#466
1 parent 4bf4f5d commit 7a9dd36

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

src/functions/RenderMarkdown.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import hljs from 'highlight.js'
22

33
import markdownIt from "markdown-it";
44
import { full as emoji } from 'markdown-it-emoji'
5+
import model_link_plugin from './markdown_plugins/ModelLink';
56

67

78

@@ -34,7 +35,9 @@ const md = markdownIt({
3435

3536
.use( require('markdown-it-footnote') )
3637

37-
.use( require('markdown-it-task-lists') );
38+
.use( require('markdown-it-task-lists') )
39+
40+
.use(model_link_plugin);
3841

3942

4043

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
};

src/index.css

+1
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ span.sub-script {
756756
font-size: smaller;
757757
}
758758

759+
span.sub-script.metadata,
759760
.ticket h3.description span.sub-script {
760761
color: #777;
761762
}

0 commit comments

Comments
 (0)