Skip to content

Commit 39a5fb0

Browse files
committed
Added support for HTML in markdown
1 parent 4c05846 commit 39a5fb0

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

utilities/markdown.js

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = function(section) {
7171
rendered = rendered.replace(/\n.*?MARKDOWNSUMMARYSTART.*?\n/g, "<summary><span class='code-details-summary-span'>");
7272
rendered = rendered.replace(/\n.*?MARKDOWNSUMMARYEND.*?\n/g, "</span></summary>");
7373
}
74-
74+
7575
return rendered;
7676
};
7777

@@ -95,7 +95,8 @@ module.exports = function(section) {
9595
xhtml: false
9696
};
9797

98-
var tokens = parseQuotes(content);
98+
var tokens = parseContent(content);
99+
tokens.links = [];
99100

100101
return marked.parser(tokens, markedDefaults);
101102
},
@@ -112,23 +113,63 @@ module.exports = function(section) {
112113
};
113114
};
114115

115-
function parseQuotes(data) {
116-
var tokens = marked.lexer(data).map(function(t) {
116+
function parseContent(data) {
117+
var tokens = [];
118+
119+
marked.lexer(data).forEach(function(t) {
120+
// add custom quotes
117121
if (t.type === 'paragraph') {
118-
return parseCustomQuote(t, 'T>', 'tip') ||
122+
var quote = parseCustomQuote(t, 'T>', 'tip') ||
119123
parseCustomQuote(t, 'W>', 'warning') ||
120124
parseCustomQuote(t, '?>', 'todo') ||
121125
t;
122-
}
123126

124-
return t;
127+
tokens.push(quote);
128+
}
129+
// handle html
130+
else if (t.type === 'html') {
131+
tokens = tokens.concat(handleHTML(t));
132+
}
133+
// just add other types
134+
else {
135+
tokens.push(t);
136+
}
125137
});
126138

127-
tokens.links = [];
128-
129139
return tokens;
130140
}
131141

142+
function handleHTML(t) {
143+
var tokens = [];
144+
145+
// 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'));
147+
148+
// if only one item in codeArray, then it's already parsed
149+
if(codeArray.length == 1) {
150+
return t;
151+
}
152+
153+
codeArray.forEach(item => {
154+
// if item is not code, then check for html tags and parse accordingly
155+
if (item.indexOf('```') !== 0) {
156+
// 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+
});
163+
}
164+
// normally parse code block
165+
else {
166+
tokens = tokens.concat(parseContent(item));
167+
}
168+
});
169+
170+
return tokens;
171+
}
172+
132173
function parseCustomQuote(token, match, className) {
133174
if (token.type === 'paragraph') {
134175
var text = token.text;

0 commit comments

Comments
 (0)