Compare commits
	
		
			25 Commits
		
	
	
		
			v0.1.13
			...
			1dcc58c4b2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1dcc58c4b2 | |||
| 78f99afef0 | |||
| 9b25d2f22c | |||
| a9e51d837d | |||
| 0800247b65 | |||
| 704447f7c0 | |||
| e40db445a5 | |||
| 4274045fca | |||
| 95c15ee027 | |||
| e184b6f8b4 | |||
| e601f45836 | |||
| 8203bbe763 | |||
| 2bd98c788d | |||
| 96f468c4f8 | |||
| c06c01823b | |||
| 8afb5423e3 | |||
| 0f83a56299 | |||
| ae7b491107 | |||
| 8599460702 | |||
| fb52d31090 | |||
| 859064f00b | |||
| 7803b495c2 | |||
| f48c0c6b75 | |||
| 34cacb48bb | |||
| 7d5d29dade | 
| @@ -1,2 +1,2 @@ | ||||
| #!/bin/sh | ||||
| node $(dirname $0)/markdown2html.js $* | ||||
| node $(dirname "$(readlink -f "$0")")/markdown2html.js $* | ||||
| @@ -1,2 +1,2 @@ | ||||
| #!/bin/sh | ||||
| node $(dirname $0)/strip-frontmatter.js $* | ||||
| node $(dirname "$(readlink -f "$0")")/strip-frontmatter.js $* | ||||
							
								
								
									
										34
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										34
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -1,18 +1,18 @@ | ||||
| { | ||||
|   "name": "@doc-utils/markdown2html", | ||||
|   "version": "0.1.13", | ||||
|   "version": "0.2.4", | ||||
|   "lockfileVersion": 2, | ||||
|   "requires": true, | ||||
|   "packages": { | ||||
|     "": { | ||||
|       "name": "@doc-utils/markdown2html", | ||||
|       "version": "0.1.13", | ||||
|       "version": "0.2.4", | ||||
|       "dependencies": { | ||||
|         "bytefield-svg": "^1.6.1", | ||||
|         "dompurify": "^2.3.6", | ||||
|         "jsdom": "^20.0.1", | ||||
|         "katex": "^0.16.7", | ||||
|         "marked": "^4.1.1", | ||||
|         "marked": "^5.0.2", | ||||
|         "nomnoml": "^1.5.2", | ||||
|         "pikchr": "^0.0.5", | ||||
|         "prismjs": "^1.29.0", | ||||
| @@ -28,7 +28,7 @@ | ||||
|         "@types/jsdom": "^20.0.0", | ||||
|         "@types/katex": "^0.16.0", | ||||
|         "@types/luxon": "^3.1.0", | ||||
|         "@types/marked": "^4.0.3", | ||||
|         "@types/marked": "^5.0.0", | ||||
|         "@types/node": "^18.11.18", | ||||
|         "@types/prismjs": "^1.26.0", | ||||
|         "@types/qrcode": "^1.5.0", | ||||
| @@ -86,9 +86,9 @@ | ||||
|       "dev": true | ||||
|     }, | ||||
|     "node_modules/@types/marked": { | ||||
|       "version": "4.3.0", | ||||
|       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz", | ||||
|       "integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==", | ||||
|       "version": "5.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-5.0.0.tgz", | ||||
|       "integrity": "sha512-YcZe50jhltsCq7rc9MNZC/4QB/OnA2Pd6hrOSTOFajtabN+38slqgDDCeE/0F83SjkKBQcsZUj7VLWR0H5cKRA==", | ||||
|       "dev": true | ||||
|     }, | ||||
|     "node_modules/@types/node": { | ||||
| @@ -954,14 +954,14 @@ | ||||
|       "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" | ||||
|     }, | ||||
|     "node_modules/marked": { | ||||
|       "version": "4.3.0", | ||||
|       "resolved": "https://registry.npmjs.org/marked/-/marked-4.3.0.tgz", | ||||
|       "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==", | ||||
|       "version": "5.0.2", | ||||
|       "resolved": "https://registry.npmjs.org/marked/-/marked-5.0.2.tgz", | ||||
|       "integrity": "sha512-TXksm9GwqXCRNbFUZmMtqNLvy3K2cQHuWmyBDLOrY1e6i9UvZpOTJXoz7fBjYkJkaUFzV9hBFxMuZSyQt8R6KQ==", | ||||
|       "bin": { | ||||
|         "marked": "bin/marked.js" | ||||
|       }, | ||||
|       "engines": { | ||||
|         "node": ">= 12" | ||||
|         "node": ">= 18" | ||||
|       } | ||||
|     }, | ||||
|     "node_modules/mime-db": { | ||||
| @@ -2015,9 +2015,9 @@ | ||||
|       "dev": true | ||||
|     }, | ||||
|     "@types/marked": { | ||||
|       "version": "4.3.0", | ||||
|       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz", | ||||
|       "integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==", | ||||
|       "version": "5.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-5.0.0.tgz", | ||||
|       "integrity": "sha512-YcZe50jhltsCq7rc9MNZC/4QB/OnA2Pd6hrOSTOFajtabN+38slqgDDCeE/0F83SjkKBQcsZUj7VLWR0H5cKRA==", | ||||
|       "dev": true | ||||
|     }, | ||||
|     "@types/node": { | ||||
| @@ -2651,9 +2651,9 @@ | ||||
|       "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" | ||||
|     }, | ||||
|     "marked": { | ||||
|       "version": "4.3.0", | ||||
|       "resolved": "https://registry.npmjs.org/marked/-/marked-4.3.0.tgz", | ||||
|       "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==" | ||||
|       "version": "5.0.2", | ||||
|       "resolved": "https://registry.npmjs.org/marked/-/marked-5.0.2.tgz", | ||||
|       "integrity": "sha512-TXksm9GwqXCRNbFUZmMtqNLvy3K2cQHuWmyBDLOrY1e6i9UvZpOTJXoz7fBjYkJkaUFzV9hBFxMuZSyQt8R6KQ==" | ||||
|     }, | ||||
|     "mime-db": { | ||||
|       "version": "1.52.0", | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "@doc-utils/markdown2html", | ||||
|   "version": "0.1.13", | ||||
|   "version": "0.2.4", | ||||
|   "publishConfig": { | ||||
|     "registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/" | ||||
|   }, | ||||
| @@ -17,7 +17,7 @@ | ||||
|     "@types/jsdom": "^20.0.0", | ||||
|     "@types/katex": "^0.16.0", | ||||
|     "@types/luxon": "^3.1.0", | ||||
|     "@types/marked": "^4.0.3", | ||||
|     "@types/marked": "^5.0.0", | ||||
|     "@types/node": "^18.11.18", | ||||
|     "@types/prismjs": "^1.26.0", | ||||
|     "@types/qrcode": "^1.5.0", | ||||
| @@ -28,7 +28,7 @@ | ||||
|     "dompurify": "^2.3.6", | ||||
|     "jsdom": "^20.0.1", | ||||
|     "katex": "^0.16.7", | ||||
|     "marked": "^4.1.1", | ||||
|     "marked": "^5.0.2", | ||||
|     "nomnoml": "^1.5.2", | ||||
|     "pikchr": "^0.0.5", | ||||
|     "prismjs": "^1.29.0", | ||||
|   | ||||
							
								
								
									
										11
									
								
								src/base-url.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/base-url.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
|  | ||||
| import { marked } from 'marked'; | ||||
| import { MarkdownOptions } from './render'; | ||||
|  | ||||
| export function base_url_walk_tokens(token: marked.Token, options: MarkdownOptions) { | ||||
| 	if (options.base_url) { | ||||
| 		if (token.type === 'link' || token.type === 'image') { | ||||
| 			token.href = (new URL(token.href, options.base_url)).toString(); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										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 dom_purify = createDOMPurify(window as any as Window); | ||||
| 	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 { 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,8 @@ import { katex_block_ext, katex_inline_ext } from './katex'; | ||||
| import { footnote_list_ext, footnote_ref_ext } from './footnotes'; | ||||
| import { description_list_ext } from './description-list'; | ||||
| import { resolve_async_bindings } from './async-steps'; | ||||
| import { breadcrumb_nav_ext } from './breadcrumb-nav'; | ||||
| import { base_url_walk_tokens } from './base-url'; | ||||
|  | ||||
| export interface MarkdownOptions { | ||||
| 	base_url?: string; | ||||
| @@ -24,13 +26,36 @@ export interface MarkdownExtension { | ||||
| } | ||||
|  | ||||
| export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) { | ||||
| 	const marked_options: marked.MarkedOptions = { | ||||
| 		baseUrl: options.base_url, | ||||
| 	const marked_options = marked_opts(true, options); | ||||
| 	const unsafe_html = options.inline | ||||
| 		? marked.parseInline(markdown, marked_options) | ||||
| 		: await marked.parse(markdown, marked_options).then(resolve_async_bindings); | ||||
|  | ||||
| 	return sanitize_html(unsafe_html, options.custom_elements); | ||||
| } | ||||
|  | ||||
| export function render_markdown_to_html_inline_sync(markdown: string, options: MarkdownOptions = { }) { | ||||
| 	const marked_options = marked_opts(false, options); | ||||
| 	setup_marked(options, marked_options); | ||||
| 	const unsafe_html = marked.parseInline(markdown, marked_options); | ||||
| 	return sanitize_html(unsafe_html, options.custom_elements); | ||||
| } | ||||
|  | ||||
| function marked_opts<T extends boolean>(async: T, options: MarkdownOptions) : marked.MarkedOptions & { async: T } { | ||||
| 	return { | ||||
| 		async, | ||||
| 		breaks: options.breaks || false, | ||||
| 		renderer: create_renderer(options), | ||||
| 		mangle: false, | ||||
| 		headerIds: false, | ||||
| 	}; | ||||
| } | ||||
|  | ||||
| function setup_marked(options: MarkdownOptions, marked_options: marked.MarkedOptions) { | ||||
| 	marked.use({ | ||||
| 		walkTokens(token) { | ||||
| 			base_url_walk_tokens(token, options); | ||||
| 		}, | ||||
| 		extensions: [ | ||||
| 			katex_block_ext(marked_options.renderer, options), | ||||
| 			katex_inline_ext(marked_options.renderer, options), | ||||
| @@ -40,6 +65,7 @@ export async function render_markdown_to_html(markdown: string, options: Markdow | ||||
| 			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); | ||||
| 			}), | ||||
| @@ -52,18 +78,4 @@ export async function render_markdown_to_html(markdown: string, options: Markdow | ||||
| 			} | ||||
| 		}, | ||||
| 	}); | ||||
| 	 | ||||
| 	const unsafe_html = options.inline | ||||
| 		? marked.parseInline(markdown, marked_options) | ||||
| 		: await new Promise<string>((resolve, reject) => { | ||||
| 			marked.parse(markdown, marked_options, (error, unsafe_html) => { | ||||
| 				if (error) { | ||||
| 					return reject(error); | ||||
| 				} | ||||
|  | ||||
| 				resolve_async_bindings(unsafe_html).then(resolve, reject); | ||||
| 			}); | ||||
| 		}); | ||||
|  | ||||
| 	return sanitize_html(unsafe_html, options.custom_elements); | ||||
| } | ||||
|   | ||||
| @@ -28,23 +28,16 @@ export function create_renderer(opts: MarkdownOptions) { | ||||
| } | ||||
|  | ||||
| function heading(renderer: marked.Renderer, opts: MarkdownOptions) { | ||||
| 	return function(orig_text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string, slugger) { | ||||
| 	return function(orig_text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string) { | ||||
| 		let { text, id, html_attrs } = parse_attributes(raw); | ||||
| 	 | ||||
| 		if (! id) { | ||||
| 			id = slugger.slug(text); | ||||
| 			html_attrs.push(`id="${id}"`); | ||||
| 		if (id) { | ||||
| 			text += `\n<a class="heading-anchor" href="#${id}">` | ||||
| 				+ `\n\t\t${icons.link}` | ||||
| 				+ `\n\t\t<span style="display: none">Section titled ${text}</span>` | ||||
| 				+ `\n\t</a>`; | ||||
| 		} | ||||
| 		 | ||||
| 		return ` | ||||
| <h${level} ${html_attrs.join(' ')}> | ||||
| 	${text} | ||||
| 	<a class="heading-anchor" href="#${id}"> | ||||
| 		${icons.link} | ||||
| 		<span style="display: none">Section titled ${text}</span> | ||||
| 	</a> | ||||
| </h${level}> | ||||
| `; | ||||
| 		return `\n<h${level} ${html_attrs.join(' ')}>\n\t${text}\n</h${level}>`; | ||||
| 	}; | ||||
| } | ||||
|  | ||||
| @@ -197,7 +190,7 @@ function code(renderer: marked.Renderer, opts: MarkdownOptions) { | ||||
|  | ||||
| const arg_pattern = /^(?:[a-zA-Z0-9_:-]+|"(?:[^"\n]|(?<=\\)")*")/; | ||||
|  | ||||
| function parse_code_args(text: string) { | ||||
| function parse_code_args(text = '') { | ||||
| 	const args: string[] = [ ]; | ||||
|  | ||||
| 	text = text.trim(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user