Skip to content

Commit a3dfcf8

Browse files
committed
render markdwon by internal state
1 parent 55ea8f5 commit a3dfcf8

1 file changed

Lines changed: 41 additions & 32 deletions

File tree

src/E/e-markdown.js

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,6 @@ export default class EMarkdown extends HTMLElement {
3030
run() {
3131
const state = getNodeScopedState(this)
3232

33-
// --- Progress start ---
34-
if (this.hasAttribute('data-actions-on-progress-start')) {
35-
evaluateActionsOnProgress(
36-
this.getAttribute('data-actions-on-progress-start'),
37-
this,
38-
state
39-
)
40-
}
41-
42-
if (!this.hasAttribute('data-src')) {
43-
throw new Error('e-markdown must have "data-src" attribute')
44-
}
45-
4633
// --- Resolve showdown extensions (global registry) ---
4734
const extensions = window.__EHTML_SHOWDOWN_EXTENSIONS__ || []
4835

@@ -71,6 +58,24 @@ export default class EMarkdown extends HTMLElement {
7158
)
7259
}
7360

61+
if (this.internalState) {
62+
this.renderMarkdown(this.internalState)
63+
return
64+
}
65+
66+
// --- Progress start ---
67+
if (this.hasAttribute('data-actions-on-progress-start')) {
68+
evaluateActionsOnProgress(
69+
this.getAttribute('data-actions-on-progress-start'),
70+
this,
71+
state
72+
)
73+
}
74+
75+
if (!this.hasAttribute('data-src')) {
76+
throw new Error('e-markdown must have "data-src" attribute')
77+
}
78+
7479
// --- AJAX request ---
7580
const url = encodeURI(
7681
evaluatedStringWithParamsFromState(
@@ -100,25 +105,7 @@ export default class EMarkdown extends HTMLElement {
100105

101106
const markdown = resObj.body
102107

103-
// --- Render markdown ---
104-
if (showdown) {
105-
showdown.setFlavor('github')
106-
const converter = new showdown.Converter({
107-
tables: true,
108-
tasklists: true,
109-
simpleLineBreaks: true,
110-
emoji: true,
111-
moreStyling: true,
112-
github: true,
113-
extensions: extensions
114-
})
115-
this.innerHTML = converter.makeHtml(markdown)
116-
} else {
117-
this.innerHTML = markdown
118-
}
119-
120-
// Remove <e-markdown> wrapper
121-
unwrappedChildrenOfParent(this)
108+
this.renderMarkdown(markdown)
122109

123110
// --- Progress end ---
124111
if (this.hasAttribute('data-actions-on-progress-end')) {
@@ -133,6 +120,28 @@ export default class EMarkdown extends HTMLElement {
133120
}
134121
)
135122
}
123+
124+
renderMarkdown(markdown) {
125+
// --- Render markdown ---
126+
if (showdown) {
127+
showdown.setFlavor('github')
128+
const converter = new showdown.Converter({
129+
tables: true,
130+
tasklists: true,
131+
simpleLineBreaks: true,
132+
emoji: true,
133+
moreStyling: true,
134+
github: true,
135+
extensions: extensions
136+
})
137+
this.innerHTML = converter.makeHtml(markdown)
138+
} else {
139+
this.innerHTML = markdown
140+
}
141+
142+
// Remove <e-markdown> wrapper
143+
unwrappedChildrenOfParent(this)
144+
}
136145
}
137146

138147
customElements.define('e-markdown', EMarkdown)

0 commit comments

Comments
 (0)