mirror of https://github.com/hrfee/jfa-go
doesn't work any better, but more efficient and doesn't require eyebleach after viewing.pull/75/head
parent
9875458b01
commit
a0a25d64f1
@ -1,49 +1,40 @@
|
|||||||
const removeMd = require("remove-markdown");
|
const removeMd = require("remove-markdown");
|
||||||
|
|
||||||
export function stripMarkdown(md: string): string {
|
function stripAltText(md: string): string {
|
||||||
let foundOpenSquare = false;
|
let altStart = -1; // Start of alt text (between '[' & ']')
|
||||||
let openSquare = -1;
|
let urlStart = -1; // Start of url (between '(' & ')')
|
||||||
let openBracket = -1;
|
let urlEnd = -1;
|
||||||
let closeBracket = -1;
|
let prevURLEnd = -2;
|
||||||
let openSquares: number[] = [];
|
let out = "";
|
||||||
let closeBrackets: number[] = [];
|
|
||||||
let links: string[] = [];
|
|
||||||
let foundOpen = false;
|
|
||||||
for (let i = 0; i < md.length; i++) {
|
for (let i = 0; i < md.length; i++) {
|
||||||
const c = md.charAt(i);
|
if (altStart != -1 && urlStart != -1 && md.charAt(i) == ')') {
|
||||||
if (!foundOpenSquare && !foundOpen && c != '[' && c != ']') {
|
urlEnd = i - 1;
|
||||||
|
out += md.substring(prevURLEnd+2, altStart-1) + md.substring(urlStart, urlEnd+1);
|
||||||
|
prevURLEnd = urlEnd;
|
||||||
|
altStart = -1;
|
||||||
|
urlStart = -1;
|
||||||
|
urlEnd = -1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (c == '[' && md.charAt(i-1) != '!') {
|
if (md.charAt(i) == '[' && altStart == -1) {
|
||||||
foundOpenSquare = true;
|
altStart = i + 1
|
||||||
openSquare = i;
|
if (i > 0 && md.charAt(i-1) == '!') {
|
||||||
} else if (c == ']') {
|
altStart--
|
||||||
if (md.charAt(i+1) == '(') {
|
|
||||||
foundOpenSquare = false;
|
|
||||||
foundOpen = true;
|
|
||||||
openBracket = i + 1;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
} else if (c == ')') {
|
|
||||||
closeBracket = i;
|
|
||||||
openSquares.push(openSquare);
|
|
||||||
closeBrackets.push(closeBracket);
|
|
||||||
links.push(md.slice(openBracket+1, closeBracket))
|
|
||||||
openBracket = -1;
|
|
||||||
closeBracket = -1;
|
|
||||||
openSquare = -1;
|
|
||||||
foundOpenSquare = false;
|
|
||||||
foundOpen = false;
|
|
||||||
}
|
}
|
||||||
}
|
if (i > 0 && md.charAt(i-1) == ']' && md.charAt(i) == '(' && urlStart == -1) {
|
||||||
let fullLinks: string[] = new Array(openSquares.length);
|
urlStart = i + 1
|
||||||
for (let i = 0; i < openSquares.length; i++) {
|
|
||||||
if (openSquares[i] != -1 && closeBrackets[i] != -1) {
|
|
||||||
fullLinks[i] = md.slice(openSquares[i], closeBrackets[i]+1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let i = 0; i < openSquares.length; i++) {
|
if (prevURLEnd + 1 != md.length - 1) {
|
||||||
md = md.replace(fullLinks[i], links[i]);
|
out += md.substring(prevURLEnd+2)
|
||||||
}
|
}
|
||||||
return removeMd(md);
|
if (out == "") {
|
||||||
|
return md
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stripMarkdown(md: string): string {
|
||||||
|
return removeMd(stripAltText(md));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue