2021-02-22 16:40:37 +00:00
|
|
|
const removeMd = require("remove-markdown");
|
|
|
|
|
2021-03-26 23:13:19 +00:00
|
|
|
function stripAltText(md: string): string {
|
|
|
|
let altStart = -1; // Start of alt text (between '[' & ']')
|
|
|
|
let urlStart = -1; // Start of url (between '(' & ')')
|
|
|
|
let urlEnd = -1;
|
|
|
|
let prevURLEnd = -2;
|
|
|
|
let out = "";
|
2021-02-22 16:40:37 +00:00
|
|
|
for (let i = 0; i < md.length; i++) {
|
2021-03-26 23:13:19 +00:00
|
|
|
if (altStart != -1 && urlStart != -1 && md.charAt(i) == ')') {
|
|
|
|
urlEnd = i - 1;
|
|
|
|
out += md.substring(prevURLEnd+2, altStart-1) + md.substring(urlStart, urlEnd+1);
|
|
|
|
prevURLEnd = urlEnd;
|
|
|
|
altStart = -1;
|
|
|
|
urlStart = -1;
|
|
|
|
urlEnd = -1;
|
2021-02-22 16:40:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
if (md.charAt(i) == '[' && altStart == -1) {
|
|
|
|
altStart = i + 1
|
|
|
|
if (i > 0 && md.charAt(i-1) == '!') {
|
|
|
|
altStart--
|
2021-02-22 16:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
if (i > 0 && md.charAt(i-1) == ']' && md.charAt(i) == '(' && urlStart == -1) {
|
|
|
|
urlStart = i + 1
|
2021-02-22 16:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
if (prevURLEnd + 1 != md.length - 1) {
|
|
|
|
out += md.substring(prevURLEnd+2)
|
|
|
|
}
|
|
|
|
if (out == "") {
|
|
|
|
return md
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stripMarkdown(md: string): string {
|
|
|
|
return removeMd(stripAltText(md));
|
2021-02-22 16:40:37 +00:00
|
|
|
}
|