Skip to content

Commit 8cb938b

Browse files
committed
Added support for single tickle code
1 parent 39a5fb0 commit 8cb938b

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

utilities/markdown.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,42 @@ function parseContent(data) {
139139
return tokens;
140140
}
141141

142+
function handleHTMLSplit(tokens, htmlArray, merging) {
143+
const htmlItem = htmlArray[0];
144+
htmlArray = htmlArray.slice(1);
145+
const tickSplit = htmlItem.split('`');
146+
const tickLength = tickSplit.length;
147+
148+
// detect start of the inline code
149+
if(merging.length === 0 && tickLength%2 === 0) {
150+
merging = htmlItem;
151+
}
152+
// append code inside the inline code
153+
else if(merging.length > 0 && tickLength === 1) {
154+
merging += htmlItem;
155+
}
156+
// finish inline code
157+
else if(merging.length > 0 && tickLength > 1) {
158+
htmlArray.unshift(tickSplit.slice(1, tickLength).join("`"));
159+
merging += tickSplit[0]+"`";
160+
tokens = tokens.concat(parseContent(merging));
161+
merging = "";
162+
} else if (merging.length === 0) {
163+
tokens = tokens.concat(parseContent(htmlItem));
164+
}
165+
166+
if(htmlArray.length === 0) {
167+
return tokens;
168+
}
169+
170+
return handleHTMLSplit(tokens, htmlArray, merging);
171+
}
172+
142173
function handleHTML(t) {
143-
var tokens = [];
174+
let tokens = [];
144175

145176
// Split code in markdown, so that HTML inside code is not parsed
146-
var codeArray = t.text.split(/(```(.|\n)*```)/g).filter(v => (v !== '' && v !== '\n'));
177+
const codeArray = t.text.split(/(```(.|\n)*```)/g).filter(v => (v && v !== '' && v !== '\n'));
147178

148179
// if only one item in codeArray, then it's already parsed
149180
if(codeArray.length == 1) {
@@ -154,12 +185,8 @@ function handleHTML(t) {
154185
// if item is not code, then check for html tags and parse accordingly
155186
if (item.indexOf('```') !== 0) {
156187
// split all html tags
157-
var htmlArray = item.split(/\s*(<[^>]*>)/g).filter(v => (v !== '' && v !== '\n'));
158-
159-
// handle every single item separately
160-
htmlArray.forEach(function (html) {
161-
tokens = tokens.concat(parseContent(html));
162-
});
188+
const htmlArray = item.split(/\s*(<[^>]*>)/g).filter(v => (v !== '' && v !== '\n'));
189+
tokens = handleHTMLSplit(tokens, htmlArray, "");
163190
}
164191
// normally parse code block
165192
else {

0 commit comments

Comments
 (0)