add support for breadcrumb navs
This commit is contained in:
parent
6ad670ba07
commit
7d5d29dade
60
src/breadcrumb-nav.ts
Normal file
60
src/breadcrumb-nav.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
|
||||||
|
import { marked } from 'marked';
|
||||||
|
import { ParsedAttributes, parse_attributes } from './attrs';
|
||||||
|
import { MarkdownOptions } from './render';
|
||||||
|
|
||||||
|
export interface BreadcrumbNavToken extends marked.Tokens.Generic {
|
||||||
|
text: string;
|
||||||
|
attrs: ParsedAttributes;
|
||||||
|
items: marked.Token[][];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function breadcrumb_nav_ext(renderer: marked.Renderer, opts: MarkdownOptions) : marked.TokenizerExtension & marked.RendererExtension {
|
||||||
|
return {
|
||||||
|
name: 'breadcrumb_nav',
|
||||||
|
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: BreadcrumbNavToken = {
|
||||||
|
type: 'breadcrumb_nav',
|
||||||
|
raw: match[0],
|
||||||
|
text: match[3],
|
||||||
|
attrs: parse_attributes(match[2] || ''),
|
||||||
|
tokens: [ ],
|
||||||
|
items: [ ],
|
||||||
|
};
|
||||||
|
|
||||||
|
const lines = match[3].trim().split('\n');
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
const tokens = this.lexer.inlineTokens(line, [ ]);
|
||||||
|
token.tokens.push(...tokens);
|
||||||
|
token.items.push(tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
renderer(token: BreadcrumbNavToken) {
|
||||||
|
return `<nav aria-label="breadcrumbs" ${token.attrs.html_attrs.join(' ')}>\n`
|
||||||
|
+ `\t<ol>\n`
|
||||||
|
+ '\t\t'
|
||||||
|
+ token.items.map((tokens, index) =>{
|
||||||
|
let item = '<li>';
|
||||||
|
|
||||||
|
if (index) {
|
||||||
|
item += '<span class="separator" aria-hidden="true">/</span> ';
|
||||||
|
}
|
||||||
|
|
||||||
|
return item + this.parser.parseInline(tokens, renderer) + '</li>';
|
||||||
|
}).join('\n\t\t')
|
||||||
|
+ '\n'
|
||||||
|
+ `\t</ol>\n`
|
||||||
|
+ `</nav>`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -9,6 +9,7 @@ import { katex_block_ext, katex_inline_ext } from './katex';
|
|||||||
import { footnote_list_ext, footnote_ref_ext } from './footnotes';
|
import { footnote_list_ext, footnote_ref_ext } from './footnotes';
|
||||||
import { description_list_ext } from './description-list';
|
import { description_list_ext } from './description-list';
|
||||||
import { resolve_async_bindings } from './async-steps';
|
import { resolve_async_bindings } from './async-steps';
|
||||||
|
import { breadcrumb_nav_ext } from './breadcrumb-nav';
|
||||||
|
|
||||||
export interface MarkdownOptions {
|
export interface MarkdownOptions {
|
||||||
base_url?: string;
|
base_url?: string;
|
||||||
@ -40,6 +41,7 @@ export async function render_markdown_to_html(markdown: string, options: Markdow
|
|||||||
description_list_ext(marked_options.renderer, options),
|
description_list_ext(marked_options.renderer, options),
|
||||||
section_ext(marked_options.renderer, options),
|
section_ext(marked_options.renderer, options),
|
||||||
icon_ext(marked_options.renderer, options),
|
icon_ext(marked_options.renderer, options),
|
||||||
|
breadcrumb_nav_ext(marked_options.renderer, options),
|
||||||
...(options.extensions || [ ]).map((ext) => {
|
...(options.extensions || [ ]).map((ext) => {
|
||||||
return ext(marked_options.renderer, options);
|
return ext(marked_options.renderer, options);
|
||||||
}),
|
}),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user