Skip to content

Commit

Permalink
fix regex for code block
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanJablo committed Sep 9, 2024
1 parent e15a4d8 commit b9b6ee3
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ const UNORDERED_LIST_REGEX = /^(\s*)[-*+]\s/;
const CHECK_LIST_REGEX = /^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i;
const HEADING_REGEX = /^(#{1,6})\s/;
const QUOTE_REGEX = /^>\s/;
const CODE_START_REGEX = /^[ \t]*```(\w+)?/;
const CODE_END_REGEX = /^[ \t]*```$/;

const createBlockNode = (
createNode: (match: Array<string>) => ElementNode,
Expand Down Expand Up @@ -334,9 +336,9 @@ export const CODE: MultilineElementTransformer = {
},
regExpEnd: {
optional: true,
regExp: /[ \t]*```$/,
regExp: CODE_END_REGEX,
},
regExpStart: /^[ \t]*```(\w+)?/,
regExpStart: CODE_START_REGEX,
replace: (
rootNode,
children,
Expand Down Expand Up @@ -536,7 +538,7 @@ export function normalizeMarkdown(input: string): string {
const lastLine = sanitizedLines[sanitizedLines.length - 1];

// Detect the start or end of a code block
if (line.includes('```')) {
if (CODE_START_REGEX.test(line) || CODE_END_REGEX.test(line)) {
inCodeBlock = !inCodeBlock;
sanitizedLines.push(line);
continue;
Expand Down

0 comments on commit b9b6ee3

Please sign in to comment.