Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
e184b6f8b4
|
|||
e601f45836
|
|||
8203bbe763
|
|||
2bd98c788d
|
|||
96f468c4f8
|
|||
c06c01823b
|
|||
8afb5423e3
|
|||
0f83a56299
|
|||
ae7b491107
|
|||
8599460702
|
|||
fb52d31090
|
|||
859064f00b
|
|||
7803b495c2
|
|||
f48c0c6b75
|
|||
34cacb48bb
|
|||
7d5d29dade
|
@@ -1,2 +1,2 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
node $(dirname $0)/markdown2html.js $*
|
node $(dirname "$(readlink -f "$0")")/markdown2html.js $*
|
@@ -1,2 +1,2 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
node $(dirname $0)/strip-frontmatter.js $*
|
node $(dirname "$(readlink -f "$0")")/strip-frontmatter.js $*
|
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@doc-utils/markdown2html",
|
"name": "@doc-utils/markdown2html",
|
||||||
"version": "0.1.13",
|
"version": "0.1.21",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@doc-utils/markdown2html",
|
"name": "@doc-utils/markdown2html",
|
||||||
"version": "0.1.13",
|
"version": "0.1.21",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bytefield-svg": "^1.6.1",
|
"bytefield-svg": "^1.6.1",
|
||||||
"dompurify": "^2.3.6",
|
"dompurify": "^2.3.6",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@doc-utils/markdown2html",
|
"name": "@doc-utils/markdown2html",
|
||||||
"version": "0.1.13",
|
"version": "0.1.21",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
|
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
|
||||||
},
|
},
|
||||||
|
64
src/breadcrumb-nav.ts
Normal file
64
src/breadcrumb-nav.ts
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
|
||||||
|
import { marked } from 'marked';
|
||||||
|
import { ParsedAttributes, parse_attributes } from './attrs';
|
||||||
|
import { MarkdownOptions } from './render';
|
||||||
|
|
||||||
|
// todo: deprecate this
|
||||||
|
|
||||||
|
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>\n';
|
||||||
|
|
||||||
|
if (index) {
|
||||||
|
item += '\t\t\t<span class="separator" aria-hidden="true">/</span>\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
item += `\t\t\t${this.parser.parseInline(tokens, renderer)}\n`;
|
||||||
|
|
||||||
|
return item + '\t\t</li>';
|
||||||
|
}).join('\n\t\t')
|
||||||
|
+ '\n'
|
||||||
|
+ `\t</ol>\n`
|
||||||
|
+ `</nav>`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@@ -8,6 +8,6 @@ export function sanitize_html(html: string, custom_elements?: CustomElementHandl
|
|||||||
const { window } = new JSDOM('');
|
const { window } = new JSDOM('');
|
||||||
const dom_purify = createDOMPurify(window as any as Window);
|
const dom_purify = createDOMPurify(window as any as Window);
|
||||||
return dom_purify.sanitize(html, {
|
return dom_purify.sanitize(html, {
|
||||||
CUSTOM_ELEMENT_HANDLING: custom_elements
|
CUSTOM_ELEMENT_HANDLING: custom_elements,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,3 @@
|
|||||||
|
|
||||||
export { process_frontmatter } from './frontmatter';
|
export { process_frontmatter } from './frontmatter';
|
||||||
export { render_markdown_to_html, MarkdownExtension, MarkdownOptions } from './render';
|
export { render_markdown_to_html, render_markdown_to_html_inline_sync, MarkdownExtension, MarkdownOptions } from './render';
|
||||||
|
@@ -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);
|
||||||
}),
|
}),
|
||||||
@@ -67,3 +69,38 @@ export async function render_markdown_to_html(markdown: string, options: Markdow
|
|||||||
|
|
||||||
return sanitize_html(unsafe_html, options.custom_elements);
|
return sanitize_html(unsafe_html, options.custom_elements);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function render_markdown_to_html_inline_sync(markdown: string, options: MarkdownOptions = { }) {
|
||||||
|
const marked_options: marked.MarkedOptions = {
|
||||||
|
baseUrl: options.base_url,
|
||||||
|
breaks: options.breaks || false,
|
||||||
|
renderer: create_renderer(options),
|
||||||
|
};
|
||||||
|
|
||||||
|
marked.use({
|
||||||
|
extensions: [
|
||||||
|
katex_block_ext(marked_options.renderer, options),
|
||||||
|
katex_inline_ext(marked_options.renderer, options),
|
||||||
|
footnote_ref_ext(marked_options.renderer, options),
|
||||||
|
footnote_list_ext(marked_options.renderer, options),
|
||||||
|
mark_ext(marked_options.renderer, options),
|
||||||
|
description_list_ext(marked_options.renderer, options),
|
||||||
|
section_ext(marked_options.renderer, options),
|
||||||
|
icon_ext(marked_options.renderer, options),
|
||||||
|
breadcrumb_nav_ext(marked_options.renderer, options),
|
||||||
|
...(options.extensions || [ ]).map((ext) => {
|
||||||
|
return ext(marked_options.renderer, options);
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
tokenizer: {
|
||||||
|
url(src) {
|
||||||
|
// disable auto-linking; more can be added here to auto-link only sometimes
|
||||||
|
// see: https://github.com/markedjs/marked/issues/882#issuecomment-781628889
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const unsafe_html = marked.parseInline(markdown, marked_options);
|
||||||
|
return sanitize_html(unsafe_html, options.custom_elements);
|
||||||
|
}
|
||||||
|
@@ -197,7 +197,7 @@ function code(renderer: marked.Renderer, opts: MarkdownOptions) {
|
|||||||
|
|
||||||
const arg_pattern = /^(?:[a-zA-Z0-9_:-]+|"(?:[^"\n]|(?<=\\)")*")/;
|
const arg_pattern = /^(?:[a-zA-Z0-9_:-]+|"(?:[^"\n]|(?<=\\)")*")/;
|
||||||
|
|
||||||
function parse_code_args(text: string) {
|
function parse_code_args(text = '') {
|
||||||
const args: string[] = [ ];
|
const args: string[] = [ ];
|
||||||
|
|
||||||
text = text.trim();
|
text = text.trim();
|
||||||
|
Reference in New Issue
Block a user