38 lines
1016 B
TypeScript
38 lines
1016 B
TypeScript
|
|
import { marked } from 'marked';
|
|
import { ParsedAttributes, parse_attributes } from './attrs';
|
|
import { MarkdownOptions } from './render';
|
|
|
|
export interface SectionToken extends marked.Tokens.Generic {
|
|
text: string;
|
|
attrs: ParsedAttributes;
|
|
}
|
|
|
|
export function section_ext(renderer: marked.Renderer, opts: MarkdownOptions) : marked.TokenizerExtension & marked.RendererExtension {
|
|
return {
|
|
name: 'section',
|
|
level: 'block',
|
|
start: (src) => src.match(/^!!!/)?.index,
|
|
tokenizer(src, tokens) {
|
|
const rule = /^!!!(!*)([^\n]+)?(?:\n)((?:[^!]|!!?(?!!\1))+)!!!\1/;
|
|
const match = rule.exec(src);
|
|
|
|
if (match) {
|
|
const token: SectionToken = {
|
|
type: 'section',
|
|
raw: match[0],
|
|
text: match[3],
|
|
attrs: parse_attributes(match[2] || ''),
|
|
tokens: [ ],
|
|
};
|
|
|
|
this.lexer.blockTokens(match[3], token.tokens);
|
|
return token;
|
|
}
|
|
},
|
|
renderer(token: SectionToken) {
|
|
return `<section ${token.attrs.html_attrs.join(' ')}>${this.parser.parse(token.tokens)}</section>`;
|
|
}
|
|
};
|
|
}
|