Escape markdown sequences (#2208)
* escape inline markdown character * fix typo * improve document around custom markdown plugin and add escape sequence utils * recover inline escape sequences on edit * remove escape sequences from plain text body * use `s` for strike-through instead of del * escape block markdown sequences * fix remove escape sequence was not removing all slashes from plain text * recover block sequences on edit
This commit is contained in:
parent
b63868bbb5
commit
7456c152b7
19 changed files with 763 additions and 475 deletions
47
src/app/plugins/markdown/block/parser.ts
Normal file
47
src/app/plugins/markdown/block/parser.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { replaceMatch } from '../internal';
|
||||
import {
|
||||
BlockQuoteRule,
|
||||
CodeBlockRule,
|
||||
ESC_BLOCK_SEQ,
|
||||
HeadingRule,
|
||||
OrderedListRule,
|
||||
UnorderedListRule,
|
||||
} from './rules';
|
||||
import { runBlockRule } from './runner';
|
||||
import { BlockMDParser } from './type';
|
||||
|
||||
/**
|
||||
* Parses block-level markdown text into HTML using defined block rules.
|
||||
*
|
||||
* @param text - The markdown text to be parsed.
|
||||
* @param parseInline - Optional function to parse inline elements.
|
||||
* @returns The parsed HTML or the original text if no block-level markdown was found.
|
||||
*/
|
||||
export const parseBlockMD: BlockMDParser = (text, parseInline) => {
|
||||
if (text === '') return text;
|
||||
let result: string | undefined;
|
||||
|
||||
if (!result) result = runBlockRule(text, CodeBlockRule, parseBlockMD, parseInline);
|
||||
if (!result) result = runBlockRule(text, BlockQuoteRule, parseBlockMD, parseInline);
|
||||
if (!result) result = runBlockRule(text, OrderedListRule, parseBlockMD, parseInline);
|
||||
if (!result) result = runBlockRule(text, UnorderedListRule, parseBlockMD, parseInline);
|
||||
if (!result) result = runBlockRule(text, HeadingRule, parseBlockMD, parseInline);
|
||||
|
||||
// replace \n with <br/> because want to preserve empty lines
|
||||
if (!result) {
|
||||
result = text
|
||||
.split('\n')
|
||||
.map((lineText) => {
|
||||
const match = lineText.match(ESC_BLOCK_SEQ);
|
||||
if (!match) {
|
||||
return parseInline?.(lineText) ?? lineText;
|
||||
}
|
||||
|
||||
const [, g1] = match;
|
||||
return replaceMatch(lineText, match, g1, (t) => [parseInline?.(t) ?? t]).join('');
|
||||
})
|
||||
.join('<br/>');
|
||||
}
|
||||
|
||||
return result ?? text;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue