-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: remove external match functions * chore: update parsers
- Loading branch information
Showing
18 changed files
with
88 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.