mirror of https://github.com/usememos/memos
feat: update marked parsers (#260)
* chore: remove external match functions * chore: update parserspull/261/head
parent
4bd373ba57
commit
eaf89aa2f2
@ -1,18 +1,50 @@
|
||||
import { parserList } from "./parser";
|
||||
import { blockElementParserList, inlineElementParserList } from "./parser";
|
||||
|
||||
export const marked = (markdownStr: string, parsers = parserList) => {
|
||||
for (const parser of parsers) {
|
||||
const match = (rawStr: string, regex: RegExp): number => {
|
||||
const matchResult = rawStr.match(regex);
|
||||
if (!matchResult) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const matchStr = matchResult[0];
|
||||
return matchStr.length;
|
||||
};
|
||||
|
||||
export const marked = (markdownStr: string, blockParsers = blockElementParserList, inlineParsers = inlineElementParserList): string => {
|
||||
for (const parser of blockParsers) {
|
||||
const startIndex = markdownStr.search(parser.regex);
|
||||
const matchedLength = parser.match(markdownStr);
|
||||
const matchedLength = match(markdownStr, parser.regex);
|
||||
|
||||
if (startIndex > -1 && matchedLength > 0) {
|
||||
const prefixStr = markdownStr.slice(0, startIndex);
|
||||
const matchedStr = markdownStr.slice(startIndex, startIndex + matchedLength);
|
||||
const suffixStr = markdownStr.slice(startIndex + matchedLength);
|
||||
markdownStr = marked(prefixStr, parsers) + parser.renderer(matchedStr) + marked(suffixStr, parsers);
|
||||
break;
|
||||
return marked(prefixStr, blockParsers, inlineParsers) + parser.renderer(matchedStr) + marked(suffixStr, blockParsers, inlineParsers);
|
||||
}
|
||||
}
|
||||
|
||||
let matchedInlineParser = undefined;
|
||||
let matchedIndex = -1;
|
||||
|
||||
for (const parser of inlineElementParserList) {
|
||||
const startIndex = markdownStr.search(parser.regex);
|
||||
const matchedLength = match(markdownStr, parser.regex);
|
||||
|
||||
if (startIndex > -1 && matchedLength > 0) {
|
||||
if (!matchedInlineParser || matchedIndex > startIndex) {
|
||||
matchedIndex = startIndex;
|
||||
matchedInlineParser = parser;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchedInlineParser) {
|
||||
const matchedLength = match(markdownStr, matchedInlineParser.regex);
|
||||
const prefixStr = markdownStr.slice(0, matchedIndex);
|
||||
const matchedStr = markdownStr.slice(matchedIndex, matchedIndex + matchedLength);
|
||||
const suffixStr = markdownStr.slice(matchedIndex + matchedLength);
|
||||
return prefixStr + matchedInlineParser.renderer(matchedStr) + marked(suffixStr, [], inlineParsers);
|
||||
}
|
||||
|
||||
return markdownStr;
|
||||
};
|
||||
|
@ -1,23 +1,21 @@
|
||||
export const BOLD_REG = /\*\*([\S ]+?)\*\*/;
|
||||
import { marked } from "..";
|
||||
import Emphasis from "./Emphasis";
|
||||
import Link from "./Link";
|
||||
|
||||
const match = (rawStr: string): number => {
|
||||
export const BOLD_REG = /\*\*([\S ]+)\*\*/;
|
||||
|
||||
const renderer = (rawStr: string): string => {
|
||||
const matchResult = rawStr.match(BOLD_REG);
|
||||
if (!matchResult) {
|
||||
return 0;
|
||||
return rawStr;
|
||||
}
|
||||
|
||||
const matchStr = matchResult[0];
|
||||
return matchStr.length;
|
||||
};
|
||||
|
||||
const renderer = (rawStr: string): string => {
|
||||
const parsedStr = rawStr.replace(BOLD_REG, "<strong>$1</strong>");
|
||||
return parsedStr;
|
||||
const parsedContent = marked(matchResult[1], [], [Emphasis, Link]);
|
||||
return `<strong>${parsedContent}</strong>`;
|
||||
};
|
||||
|
||||
export default {
|
||||
name: "bold",
|
||||
regex: BOLD_REG,
|
||||
match,
|
||||
renderer,
|
||||
};
|
||||
|
@ -1,23 +1,21 @@
|
||||
export const EMPHASIS_REG = /\*([\S ]+?)\*/;
|
||||
import { marked } from "..";
|
||||
import Bold from "./Bold";
|
||||
import Link from "./Link";
|
||||
|
||||
const match = (rawStr: string): number => {
|
||||
export const EMPHASIS_REG = /\*([\S ]+)\*/;
|
||||
|
||||
const renderer = (rawStr: string): string => {
|
||||
const matchResult = rawStr.match(EMPHASIS_REG);
|
||||
if (!matchResult) {
|
||||
return 0;
|
||||
return rawStr;
|
||||
}
|
||||
|
||||
const matchStr = matchResult[0];
|
||||
return matchStr.length;
|
||||
};
|
||||
|
||||
const renderer = (rawStr: string): string => {
|
||||
const parsedStr = rawStr.replace(EMPHASIS_REG, "<em>$1</em>");
|
||||
return parsedStr;
|
||||
const parsedContent = marked(matchResult[1], [], [Bold, Link]);
|
||||
return `<em>${parsedContent}</em>`;
|
||||
};
|
||||
|
||||
export default {
|
||||
name: "emphasis",
|
||||
regex: EMPHASIS_REG,
|
||||
match,
|
||||
renderer,
|
||||
};
|
||||
|
Loading…
Reference in New Issue