Compare commits
	
		
			4 Commits
		
	
	
		
			v0.2.2
			...
			1dcc58c4b2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1dcc58c4b2 | |||
| 78f99afef0 | |||
| 9b25d2f22c | |||
| a9e51d837d | 
							
								
								
									
										4
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										4
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -1,12 +1,12 @@ | ||||
| { | ||||
|   "name": "@doc-utils/markdown2html", | ||||
|   "version": "0.2.2", | ||||
|   "version": "0.2.4", | ||||
|   "lockfileVersion": 2, | ||||
|   "requires": true, | ||||
|   "packages": { | ||||
|     "": { | ||||
|       "name": "@doc-utils/markdown2html", | ||||
|       "version": "0.2.2", | ||||
|       "version": "0.2.4", | ||||
|       "dependencies": { | ||||
|         "bytefield-svg": "^1.6.1", | ||||
|         "dompurify": "^2.3.6", | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "@doc-utils/markdown2html", | ||||
|   "version": "0.2.2", | ||||
|   "version": "0.2.4", | ||||
|   "publishConfig": { | ||||
|     "registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/" | ||||
|   }, | ||||
|   | ||||
| @@ -26,14 +26,32 @@ export interface MarkdownExtension { | ||||
| } | ||||
|  | ||||
| export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) { | ||||
| 	const marked_options: marked.MarkedOptions & { async: true } = { | ||||
| 		async: true, | ||||
| 	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); | ||||
| @@ -60,45 +78,4 @@ export async function render_markdown_to_html(markdown: string, options: Markdow | ||||
| 			} | ||||
| 		}, | ||||
| 	}); | ||||
| 	 | ||||
| 	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.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); | ||||
| } | ||||
|   | ||||
| @@ -30,16 +30,14 @@ 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) { | ||||
| 		let { text, id, html_attrs } = parse_attributes(raw); | ||||
| 		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}>`; | ||||
| 	}; | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user