-
Hello! I'm trying to parse multiline tokens, but output is not correct, time by time. What I'm doing wrong? const { marked } = require('marked');
const hintMd = {
name: 'block',
level: 'block',
start: (src) => src.match(/^@block/)?.index,
tokenizer(src) {
const blockRe = /^@block\[(?<type>.*?)]$(?<body>.*?)^@end_block\n*/gms;
const blockMatch = blockRe.exec(src);
if (!blockMatch) return;
const token = {
type: 'block',
raw: blockMatch[0],
t: blockMatch.groups.type,
tokens: [],
};
if (blockMatch.groups.body) {
this.lexer.blockTokens(blockMatch.groups.body, token.tokens);
}
return token;
},
renderer(token) {
return `<section class="block ${token.t}"></section>`;
},
};
const raw = `
before
@block[type]
- list inside
- list inside
- list inside
@end_block
@block[type]
- list inside
- list inside
- list inside
@end_block
@block[type]
- list inside
- list inside
- list inside
@end_block
@block[type]
@end_block
after
`;
marked.use({
extensions: [hintMd],
});
// marked.parse(raw)
console.log(marked.parse(raw)); Output contains additional symbols (
Live example hosted on stackblitz: https://stackblitz.com/edit/node-hfdcw3?file=index.js |
Beta Was this translation helpful? Give feedback.
Answered by
UziTech
Nov 25, 2022
Replies: 1 comment 1 reply
-
Looks like You should be able to get it to work with const blockRe = /^@block\[(?<type>[^\]]*)](?<body>[^]*?)@end_block\n*/gs; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
epszaw
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like
blockRe
matches the beginning ofsrc
even when@block
is not at the start ofsrc
because of them
flag, so it consumesbefore
.You should be able to get it to work with