Compare commits
	
		
			37 Commits
		
	
	
		
			v0.1.11
			...
			b016f77c51
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| b016f77c51 | |||
| e127195eaa | |||
| 71809455e2 | |||
| 21dbcad44f | |||
| 873aefcc6c | |||
| 5f5cd286e7 | |||
| 5304e20c94 | |||
| 865016bb26 | |||
| 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 | |||
| 6ad670ba07 | |||
| 3ddde81e1c | |||
| 44315b324a | |||
| 20cc517802 | 
| @@ -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 $* | ||||||
							
								
								
									
										58
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										58
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -1,18 +1,18 @@ | |||||||
| { | { | ||||||
|   "name": "@doc-utils/markdown2html", |   "name": "@doc-utils/markdown2html", | ||||||
|   "version": "0.1.11", |   "version": "0.3.1", | ||||||
|   "lockfileVersion": 2, |   "lockfileVersion": 2, | ||||||
|   "requires": true, |   "requires": true, | ||||||
|   "packages": { |   "packages": { | ||||||
|     "": { |     "": { | ||||||
|       "name": "@doc-utils/markdown2html", |       "name": "@doc-utils/markdown2html", | ||||||
|       "version": "0.1.11", |       "version": "0.3.1", | ||||||
|       "dependencies": { |       "dependencies": { | ||||||
|         "bytefield-svg": "^1.6.1", |         "bytefield-svg": "^1.6.1", | ||||||
|         "dompurify": "^2.3.6", |         "dompurify": "^2.3.6", | ||||||
|         "jsdom": "^20.0.1", |         "jsdom": "^20.0.1", | ||||||
|         "katex": "^0.16.7", |         "katex": "^0.16.7", | ||||||
|         "marked": "^4.1.1", |         "marked": "^5.0.2", | ||||||
|         "nomnoml": "^1.5.2", |         "nomnoml": "^1.5.2", | ||||||
|         "pikchr": "^0.0.5", |         "pikchr": "^0.0.5", | ||||||
|         "prismjs": "^1.29.0", |         "prismjs": "^1.29.0", | ||||||
| @@ -28,7 +28,7 @@ | |||||||
|         "@types/jsdom": "^20.0.0", |         "@types/jsdom": "^20.0.0", | ||||||
|         "@types/katex": "^0.16.0", |         "@types/katex": "^0.16.0", | ||||||
|         "@types/luxon": "^3.1.0", |         "@types/luxon": "^3.1.0", | ||||||
|         "@types/marked": "^4.0.3", |         "@types/marked": "^5.0.0", | ||||||
|         "@types/node": "^18.11.18", |         "@types/node": "^18.11.18", | ||||||
|         "@types/prismjs": "^1.26.0", |         "@types/prismjs": "^1.26.0", | ||||||
|         "@types/qrcode": "^1.5.0", |         "@types/qrcode": "^1.5.0", | ||||||
| @@ -86,15 +86,15 @@ | |||||||
|       "dev": true |       "dev": true | ||||||
|     }, |     }, | ||||||
|     "node_modules/@types/marked": { |     "node_modules/@types/marked": { | ||||||
|       "version": "4.3.0", |       "version": "5.0.0", | ||||||
|       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz", |       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-5.0.0.tgz", | ||||||
|       "integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==", |       "integrity": "sha512-YcZe50jhltsCq7rc9MNZC/4QB/OnA2Pd6hrOSTOFajtabN+38slqgDDCeE/0F83SjkKBQcsZUj7VLWR0H5cKRA==", | ||||||
|       "dev": true |       "dev": true | ||||||
|     }, |     }, | ||||||
|     "node_modules/@types/node": { |     "node_modules/@types/node": { | ||||||
|       "version": "18.16.5", |       "version": "18.16.6", | ||||||
|       "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.5.tgz", |       "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.6.tgz", | ||||||
|       "integrity": "sha512-seOA34WMo9KB+UA78qaJoCO20RJzZGVXQ5Sh6FWu0g/hfT44nKXnej3/tCQl7FL97idFpBhisLYCTB50S0EirA==", |       "integrity": "sha512-N7KINmeB8IN3vRR8dhgHEp+YpWvGFcpDoh5XZ8jB5a00AdFKCKEyyGTOPTddUf4JqU1ZKTVxkOxakDvchNVI2Q==", | ||||||
|       "dev": true |       "dev": true | ||||||
|     }, |     }, | ||||||
|     "node_modules/@types/prismjs": { |     "node_modules/@types/prismjs": { | ||||||
| @@ -954,14 +954,14 @@ | |||||||
|       "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" |       "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" | ||||||
|     }, |     }, | ||||||
|     "node_modules/marked": { |     "node_modules/marked": { | ||||||
|       "version": "4.3.0", |       "version": "5.0.2", | ||||||
|       "resolved": "https://registry.npmjs.org/marked/-/marked-4.3.0.tgz", |       "resolved": "https://registry.npmjs.org/marked/-/marked-5.0.2.tgz", | ||||||
|       "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==", |       "integrity": "sha512-TXksm9GwqXCRNbFUZmMtqNLvy3K2cQHuWmyBDLOrY1e6i9UvZpOTJXoz7fBjYkJkaUFzV9hBFxMuZSyQt8R6KQ==", | ||||||
|       "bin": { |       "bin": { | ||||||
|         "marked": "bin/marked.js" |         "marked": "bin/marked.js" | ||||||
|       }, |       }, | ||||||
|       "engines": { |       "engines": { | ||||||
|         "node": ">= 12" |         "node": ">= 18" | ||||||
|       } |       } | ||||||
|     }, |     }, | ||||||
|     "node_modules/mime-db": { |     "node_modules/mime-db": { | ||||||
| @@ -989,9 +989,9 @@ | |||||||
|       "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" |       "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" | ||||||
|     }, |     }, | ||||||
|     "node_modules/node-fetch": { |     "node_modules/node-fetch": { | ||||||
|       "version": "2.6.9", |       "version": "2.6.11", | ||||||
|       "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", |       "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.11.tgz", | ||||||
|       "integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==", |       "integrity": "sha512-4I6pdBY1EthSqDmJkiNk3JIT8cswwR9nfeW/cPdUagJYEQG7R95WRH74wpz7ma8Gh/9dI9FP+OU+0E4FvtA55w==", | ||||||
|       "dependencies": { |       "dependencies": { | ||||||
|         "whatwg-url": "^5.0.0" |         "whatwg-url": "^5.0.0" | ||||||
|       }, |       }, | ||||||
| @@ -2015,15 +2015,15 @@ | |||||||
|       "dev": true |       "dev": true | ||||||
|     }, |     }, | ||||||
|     "@types/marked": { |     "@types/marked": { | ||||||
|       "version": "4.3.0", |       "version": "5.0.0", | ||||||
|       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz", |       "resolved": "https://registry.npmjs.org/@types/marked/-/marked-5.0.0.tgz", | ||||||
|       "integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==", |       "integrity": "sha512-YcZe50jhltsCq7rc9MNZC/4QB/OnA2Pd6hrOSTOFajtabN+38slqgDDCeE/0F83SjkKBQcsZUj7VLWR0H5cKRA==", | ||||||
|       "dev": true |       "dev": true | ||||||
|     }, |     }, | ||||||
|     "@types/node": { |     "@types/node": { | ||||||
|       "version": "18.16.5", |       "version": "18.16.6", | ||||||
|       "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.5.tgz", |       "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.6.tgz", | ||||||
|       "integrity": "sha512-seOA34WMo9KB+UA78qaJoCO20RJzZGVXQ5Sh6FWu0g/hfT44nKXnej3/tCQl7FL97idFpBhisLYCTB50S0EirA==", |       "integrity": "sha512-N7KINmeB8IN3vRR8dhgHEp+YpWvGFcpDoh5XZ8jB5a00AdFKCKEyyGTOPTddUf4JqU1ZKTVxkOxakDvchNVI2Q==", | ||||||
|       "dev": true |       "dev": true | ||||||
|     }, |     }, | ||||||
|     "@types/prismjs": { |     "@types/prismjs": { | ||||||
| @@ -2651,9 +2651,9 @@ | |||||||
|       "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" |       "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" | ||||||
|     }, |     }, | ||||||
|     "marked": { |     "marked": { | ||||||
|       "version": "4.3.0", |       "version": "5.0.2", | ||||||
|       "resolved": "https://registry.npmjs.org/marked/-/marked-4.3.0.tgz", |       "resolved": "https://registry.npmjs.org/marked/-/marked-5.0.2.tgz", | ||||||
|       "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==" |       "integrity": "sha512-TXksm9GwqXCRNbFUZmMtqNLvy3K2cQHuWmyBDLOrY1e6i9UvZpOTJXoz7fBjYkJkaUFzV9hBFxMuZSyQt8R6KQ==" | ||||||
|     }, |     }, | ||||||
|     "mime-db": { |     "mime-db": { | ||||||
|       "version": "1.52.0", |       "version": "1.52.0", | ||||||
| @@ -2674,9 +2674,9 @@ | |||||||
|       "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" |       "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" | ||||||
|     }, |     }, | ||||||
|     "node-fetch": { |     "node-fetch": { | ||||||
|       "version": "2.6.9", |       "version": "2.6.11", | ||||||
|       "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", |       "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.11.tgz", | ||||||
|       "integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==", |       "integrity": "sha512-4I6pdBY1EthSqDmJkiNk3JIT8cswwR9nfeW/cPdUagJYEQG7R95WRH74wpz7ma8Gh/9dI9FP+OU+0E4FvtA55w==", | ||||||
|       "requires": { |       "requires": { | ||||||
|         "whatwg-url": "^5.0.0" |         "whatwg-url": "^5.0.0" | ||||||
|       }, |       }, | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "@doc-utils/markdown2html", |   "name": "@doc-utils/markdown2html", | ||||||
|   "version": "0.1.11", |   "version": "0.3.1", | ||||||
|   "publishConfig": { |   "publishConfig": { | ||||||
|     "registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/" |     "registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/" | ||||||
|   }, |   }, | ||||||
| @@ -17,7 +17,7 @@ | |||||||
|     "@types/jsdom": "^20.0.0", |     "@types/jsdom": "^20.0.0", | ||||||
|     "@types/katex": "^0.16.0", |     "@types/katex": "^0.16.0", | ||||||
|     "@types/luxon": "^3.1.0", |     "@types/luxon": "^3.1.0", | ||||||
|     "@types/marked": "^4.0.3", |     "@types/marked": "^5.0.0", | ||||||
|     "@types/node": "^18.11.18", |     "@types/node": "^18.11.18", | ||||||
|     "@types/prismjs": "^1.26.0", |     "@types/prismjs": "^1.26.0", | ||||||
|     "@types/qrcode": "^1.5.0", |     "@types/qrcode": "^1.5.0", | ||||||
| @@ -28,7 +28,7 @@ | |||||||
|     "dompurify": "^2.3.6", |     "dompurify": "^2.3.6", | ||||||
|     "jsdom": "^20.0.1", |     "jsdom": "^20.0.1", | ||||||
|     "katex": "^0.16.7", |     "katex": "^0.16.7", | ||||||
|     "marked": "^4.1.1", |     "marked": "^5.0.2", | ||||||
|     "nomnoml": "^1.5.2", |     "nomnoml": "^1.5.2", | ||||||
|     "pikchr": "^0.0.5", |     "pikchr": "^0.0.5", | ||||||
|     "prismjs": "^1.29.0", |     "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(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -3,14 +3,19 @@ import { marked } from 'marked'; | |||||||
| import { MarkdownOptions } from './render'; | import { MarkdownOptions } from './render'; | ||||||
|  |  | ||||||
| export interface DescriptionListToken extends marked.Tokens.Generic { | export interface DescriptionListToken extends marked.Tokens.Generic { | ||||||
|  | 	type: 'description_list'; | ||||||
| 	items: (DescriptionTermToken | DescriptionDetailToken)[]; | 	items: (DescriptionTermToken | DescriptionDetailToken)[]; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export type DescriptionElemToken = DescriptionTermToken | DescriptionDetailToken; | ||||||
|  |  | ||||||
| export interface DescriptionTermToken extends marked.Tokens.Generic { | export interface DescriptionTermToken extends marked.Tokens.Generic { | ||||||
|  | 	type: 'description_term'; | ||||||
| 	text: string; | 	text: string; | ||||||
| } | } | ||||||
|  |  | ||||||
| export interface DescriptionDetailToken extends marked.Tokens.Generic { | export interface DescriptionDetailToken extends marked.Tokens.Generic { | ||||||
|  | 	type: 'description_detail'; | ||||||
| 	text: string; | 	text: string; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -18,72 +23,106 @@ export function description_list_ext(renderer: marked.Renderer, opts: MarkdownOp | |||||||
| 	return { | 	return { | ||||||
| 		name: 'description_list', | 		name: 'description_list', | ||||||
| 		level: 'block', | 		level: 'block', | ||||||
| 		start: (src) => src.match(/^:[:#-]/)?.index, | 		start: (src) => src.match(/^: /)?.index, | ||||||
| 		tokenizer(src, tokens) { | 		tokenizer(src, tokens) { | ||||||
| 			const rule = /^(?::[:#-](?:\s[^\n]*)?(?:\n|$))+/; | 			const start = src.match(/^: /)?.index; | ||||||
| 			const match = rule.exec(src); | 			const lines = src.slice(start).split(/\n/g); | ||||||
|  |  | ||||||
| 			if (match) { |  | ||||||
| 			const token: DescriptionListToken = { | 			const token: DescriptionListToken = { | ||||||
| 				type: 'description_list', | 				type: 'description_list', | ||||||
| 					raw: match[0], | 				raw: '', | ||||||
| 				items: [ ] | 				items: [ ] | ||||||
| 			}; | 			}; | ||||||
|  |  | ||||||
| 				const items = token.raw.trim().split('\n'); | 			let current: DescriptionElemToken; | ||||||
| 				const raw_buffer: string[] = [ ]; | 			const render_current = () => { | ||||||
| 				const text_buffer: string[] = [ ]; | 				if (current) { | ||||||
|  | 					this.lexer.blockTokens(current.text, current.tokens); | ||||||
|  | 					current = null; | ||||||
|  | 				} | ||||||
|  | 			}; | ||||||
|  |  | ||||||
| 				const flush_buffer = () => { | 			for (const line of lines) { | ||||||
| 					if (! raw_buffer.length) { | 				// Skip empty lines | ||||||
| 						return; | 				if (! line.trim()) { | ||||||
|  | 					token.raw += line + '\n'; | ||||||
|  |  | ||||||
|  | 					if (current) { | ||||||
|  | 						current.raw += line + '\n'; | ||||||
|  | 						current.text += '\n'; | ||||||
| 					} | 					} | ||||||
|  |  | ||||||
| 					// Grab the second character from the first line to determine the | 					continue; | ||||||
| 					// token type (should be "#" or "-") | 				} | ||||||
| 					const type = raw_buffer[0][1] === '#' ? 'description_term' : 'description_detail'; |  | ||||||
|  |  | ||||||
| 					const sub_token: (DescriptionTermToken | DescriptionDetailToken) = { | 				// If the line starts immediately with a colon, it is a <dt> | ||||||
| 						type, | 				if (line.startsWith(': ')) { | ||||||
| 						raw: raw_buffer.join('\n'), | 					render_current(); | ||||||
| 						text: text_buffer.join('\n'), | 					token.raw += line + '\n'; | ||||||
|  | 					token.items.push( | ||||||
|  | 						current = { | ||||||
|  | 							type: 'description_term', | ||||||
|  | 							raw: line, | ||||||
|  | 							text: line.slice(2), | ||||||
| 							tokens: [ ], | 							tokens: [ ], | ||||||
| 					}; | 						} | ||||||
|  | 					); | ||||||
| 					raw_buffer.length = 0; | 					continue; | ||||||
| 					text_buffer.length = 0; |  | ||||||
|  |  | ||||||
| 					this.lexer.blockTokens(sub_token.text, sub_token.tokens); |  | ||||||
| 					token.items.push(sub_token); |  | ||||||
| 				}; |  | ||||||
|  |  | ||||||
| 				for (const line of items) { |  | ||||||
| 					const rule = /^:([:#-])(?:\s([^\n]*))?(?:\n|$)/; |  | ||||||
| 					const match = rule.exec(line); |  | ||||||
| 		 |  | ||||||
| 					if (match) { |  | ||||||
| 						if (match[1] !== ':') { |  | ||||||
| 							flush_buffer(); |  | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 						raw_buffer.push(match[0]); | 				// If the line starts with a colon after an indent, it is a <dd> | ||||||
| 						text_buffer.push(match[2]); | 				if (line.startsWith('  : ')) { | ||||||
|  | 					render_current(); | ||||||
|  | 					token.raw += line + '\n'; | ||||||
|  | 					token.items.push( | ||||||
|  | 						current = { | ||||||
|  | 							type: 'description_detail', | ||||||
|  | 							raw: line, | ||||||
|  | 							text: line.slice(4), | ||||||
|  | 							tokens: [ ], | ||||||
| 						} | 						} | ||||||
|  | 					); | ||||||
|  | 					continue; | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				flush_buffer(); | 				// If the line starts with (at least) two indents, it is a child | ||||||
|  | 				// of the current element | ||||||
|  | 				if (line.startsWith('    ')) { | ||||||
|  | 					token.raw += line + '\n'; | ||||||
|  | 					current.raw += '\n' + line; | ||||||
|  | 					current.text += '\n' + line.slice(current.type === 'description_term' ? 2 : 4); | ||||||
|  | 					continue; | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				// If the line starts with one indent, it is a child of the current | ||||||
|  | 				// <dt> (but is not allowed after a <dd>) | ||||||
|  | 				if (line.startsWith('  ')) { | ||||||
|  | 					if (current.type !== 'description_term') { | ||||||
|  | 						render_current(); | ||||||
|  | 						break; | ||||||
|  | 					} | ||||||
|  |  | ||||||
|  | 					token.raw += line + '\n'; | ||||||
|  | 					current.raw += '\n' + line; | ||||||
|  | 					current.text += '\n' + line.slice(2); | ||||||
|  | 					continue; | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				// If the line starts any other way, it is the start of new content | ||||||
|  | 				// and we are done parsing | ||||||
|  | 				render_current(); | ||||||
|  | 				break; | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 			render_current(); | ||||||
|  |  | ||||||
|  | 			if (token.items.length) { | ||||||
| 				return token; | 				return token; | ||||||
| 			} | 			} | ||||||
| 		}, | 		}, | ||||||
| 		renderer(token: DescriptionListToken) { | 		renderer(token: DescriptionListToken) { | ||||||
| 			const items = token.items.map((item) => { | 			const items = token.items.map((item) => { | ||||||
| 				const tag = item.type === 'description_term' ? 'dt' : 'dd'; | 				const tag = item.type === 'description_term' ? 'dt' : 'dd'; | ||||||
| 				return ` | 				return `<${tag}>${this.parser.parse(item.tokens)}</${tag}>`; | ||||||
| 					<${tag}> |  | ||||||
| 						${this.parser.parse(item.tokens)} |  | ||||||
| 					</${tag}> |  | ||||||
| 				`; |  | ||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
| 			return `<dl>${items.join('')}</dl>`; | 			return `<dl>${items.join('')}</dl>`; | ||||||
|   | |||||||
| @@ -2,13 +2,12 @@ | |||||||
| import { JSDOM } from 'jsdom'; | import { JSDOM } from 'jsdom'; | ||||||
| import createDOMPurify = require('dompurify'); | import createDOMPurify = require('dompurify'); | ||||||
|  |  | ||||||
| export function sanitize_html(html: string) : string { | export type CustomElementHandling = createDOMPurify.Config['CUSTOM_ELEMENT_HANDLING']; | ||||||
|  |  | ||||||
|  | export function sanitize_html(html: string, custom_elements?: CustomElementHandling) : string { | ||||||
| 	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_ELEMENT_HANDLING: custom_elements, | ||||||
| 			tagNameCheck: (tag_name) => tag_name === 'svg-icon', |  | ||||||
| 			attributeNameCheck: (attr_name) => attr_name === 'icon', |  | ||||||
| 		} |  | ||||||
| 	}); | 	}); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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'; | ||||||
|   | |||||||
| @@ -92,16 +92,16 @@ const shape_fill_regex = { | |||||||
| const arrow_head_regex: Record<CSSVarColor, RegExp> = { | const arrow_head_regex: Record<CSSVarColor, RegExp> = { | ||||||
| 	black:    /fill:rgb\(0,0,0\)/gi, | 	black:    /fill:rgb\(0,0,0\)/gi, | ||||||
| 	red:      /fill:rgb\(255,0,0\)/gi, | 	red:      /fill:rgb\(255,0,0\)/gi, | ||||||
| 	orange:   /fill:rgb:\(255,165,0\)/gi, | 	orange:   /fill:rgb\(255,165,0\)/gi, | ||||||
| 	yellow:   /fill:rgb:\(255,255,0\)/gi, | 	yellow:   /fill:rgb\(255,255,0\)/gi, | ||||||
| 	green:    /fill:rgb:\(0,128,0\)/gi, | 	green:    /fill:rgb\(0,128,0\)/gi, | ||||||
| 	teal:     /fill:rgb:\(0,128,128\)/gi, | 	teal:     /fill:rgb\(0,128,128\)/gi, | ||||||
| 	pink:     /fill:rgb:\(255,192,203\)/gi, | 	pink:     /fill:rgb\(255,192,203\)/gi, | ||||||
| 	purple:   /fill:rgb:\(128,0,128\)/gi, | 	purple:   /fill:rgb\(128,0,128\)/gi, | ||||||
| 	blue:     /fill:rgb:\(0,0,255\)/gi, | 	blue:     /fill:rgb\(0,0,255\)/gi, | ||||||
| 	indigo:   /fill:rgb:\(75,0,130\)/gi, | 	indigo:   /fill:rgb\(75,0,130\)/gi, | ||||||
| 	magenta:  /fill:rgb:\(255,0,255\)/gi, | 	magenta:  /fill:rgb\(255,0,255\)/gi, | ||||||
| 	brown:    /fill:rgb:\(165,42,42\)/gi, | 	brown:    /fill:rgb\(165,42,42\)/gi, | ||||||
| }; | }; | ||||||
|  |  | ||||||
| const line_css_vars: Record<CSSVarColor, string> = { | const line_css_vars: Record<CSSVarColor, string> = { | ||||||
|   | |||||||
| @@ -4,11 +4,12 @@ import { create_renderer } from './renderer'; | |||||||
| import { mark_ext } from './mark'; | import { mark_ext } from './mark'; | ||||||
| import { section_ext } from './section'; | import { section_ext } from './section'; | ||||||
| import { icon_ext } from './icon'; | import { icon_ext } from './icon'; | ||||||
| import { sanitize_html } from './html-sanitize'; | import { CustomElementHandling, sanitize_html } from './html-sanitize'; | ||||||
| import { katex_block_ext, katex_inline_ext } from './katex'; | 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 { base_url_walk_tokens } from './base-url'; | ||||||
|  |  | ||||||
| export interface MarkdownOptions { | export interface MarkdownOptions { | ||||||
| 	base_url?: string; | 	base_url?: string; | ||||||
| @@ -16,6 +17,7 @@ export interface MarkdownOptions { | |||||||
| 	inline?: boolean; | 	inline?: boolean; | ||||||
| 	katex_macros?: Record<string, string>; | 	katex_macros?: Record<string, string>; | ||||||
| 	extensions?: MarkdownExtension[]; | 	extensions?: MarkdownExtension[]; | ||||||
|  | 	custom_elements?: CustomElementHandling; | ||||||
| } | } | ||||||
|  |  | ||||||
| export interface MarkdownExtension { | export interface MarkdownExtension { | ||||||
| @@ -23,13 +25,36 @@ export interface MarkdownExtension { | |||||||
| } | } | ||||||
|  |  | ||||||
| export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) { | export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) { | ||||||
| 	const marked_options: marked.MarkedOptions = { | 	const marked_options = marked_opts(options); | ||||||
| 		baseUrl: options.base_url, | 	setup_marked(options, marked_options); | ||||||
|  | 	const unsafe_html = options.inline | ||||||
|  | 		? marked.parseInline(markdown, marked_options) | ||||||
|  | 		: marked.parse(markdown, marked_options); | ||||||
|  |  | ||||||
|  | 	return sanitize_html(await resolve_async_bindings(unsafe_html), options.custom_elements); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export function render_markdown_to_html_inline_sync(markdown: string, options: MarkdownOptions = { }) { | ||||||
|  | 	const marked_options = marked_opts(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>(options: MarkdownOptions) : marked.MarkedOptions { | ||||||
|  | 	return { | ||||||
| 		breaks: options.breaks || false, | 		breaks: options.breaks || false, | ||||||
| 		renderer: create_renderer(options), | 		renderer: create_renderer(options), | ||||||
|  | 		mangle: false, | ||||||
|  | 		headerIds: false, | ||||||
| 	}; | 	}; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function setup_marked(options: MarkdownOptions, marked_options: marked.MarkedOptions) { | ||||||
| 	marked.use({ | 	marked.use({ | ||||||
|  | 		walkTokens(token) { | ||||||
|  | 			base_url_walk_tokens(token, options); | ||||||
|  | 		}, | ||||||
| 		extensions: [ | 		extensions: [ | ||||||
| 			katex_block_ext(marked_options.renderer, options), | 			katex_block_ext(marked_options.renderer, options), | ||||||
| 			katex_inline_ext(marked_options.renderer, options), | 			katex_inline_ext(marked_options.renderer, options), | ||||||
| @@ -51,18 +76,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); |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -28,23 +28,16 @@ export function create_renderer(opts: MarkdownOptions) { | |||||||
| } | } | ||||||
|  |  | ||||||
| function heading(renderer: marked.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); | 		let { text, id, html_attrs } = parse_attributes(raw); | ||||||
| 	 | 		if (id) { | ||||||
| 		if (! id) { | 			text += `\n<a class="heading-anchor" href="#${id}">` | ||||||
| 			id = slugger.slug(text); | 				+ `\n\t\t${icons.link}` | ||||||
| 			html_attrs.push(`id="${id}"`); | 				+ `\n\t\t<span style="display: none">Section titled ${text}</span>` | ||||||
|  | 				+ `\n\t</a>`; | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		return ` | 		return `\n<h${level} ${html_attrs.join(' ')}>\n\t${text}\n</h${level}>`; | ||||||
| <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}> |  | ||||||
| `; |  | ||||||
| 	}; | 	}; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -91,6 +84,8 @@ function code(renderer: marked.Renderer, opts: MarkdownOptions) { | |||||||
| 				// Find the first newline that is not preceeded by a "\" | 				// Find the first newline that is not preceeded by a "\" | ||||||
| 				const end_of_input = /(?<!\\)(?:\r\n|\r|\n)/.exec(code); | 				const end_of_input = /(?<!\\)(?:\r\n|\r|\n)/.exec(code); | ||||||
|  |  | ||||||
|  | 				// todo: handling for multi-line heredocs? | ||||||
|  | 	 | ||||||
| 				// If there is no such newline, the whole content is input | 				// If there is no such newline, the whole content is input | ||||||
| 				if (! end_of_input) { | 				if (! end_of_input) { | ||||||
| 					return figure(`<pre class="language-bash">${render_prism(code, 'bash')}</pre>`); | 					return figure(`<pre class="language-bash">${render_prism(code, 'bash')}</pre>`); | ||||||
| @@ -158,6 +153,10 @@ function code(renderer: marked.Renderer, opts: MarkdownOptions) { | |||||||
| 				return figure(binding); | 				return figure(binding); | ||||||
| 			}; | 			}; | ||||||
|  |  | ||||||
|  | 			case 'yaml:calendar': { | ||||||
|  | 				// todo | ||||||
|  | 			}; | ||||||
|  | 	 | ||||||
| 			default: | 			default: | ||||||
| 				return figure(`<pre class="language-${args[0] || 'txt'}">${render_prism(code, args[0])}</pre>`); | 				return figure(`<pre class="language-${args[0] || 'txt'}">${render_prism(code, args[0])}</pre>`); | ||||||
| 		} | 		} | ||||||
| @@ -195,7 +194,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(); | ||||||
|   | |||||||
| @@ -1,18 +1,27 @@ | |||||||
|  |  | ||||||
| // note: fallback colors come from "category10" scheme |  | ||||||
| // https://vega.github.io/vega/docs/schemes/#category10 |  | ||||||
| // todo: css variables |  | ||||||
| export const chart_data_colors = [ | export const chart_data_colors = [ | ||||||
| 	'var(--theme-chart-data-0, #1f77b4)', | 	'var(--theme-chart-shape-red-fill, #feaea5)', | ||||||
| 	'var(--theme-chart-data-1, #ff7f0e)', | 	'var(--theme-chart-shape-orange-fill, #fad6bc)', | ||||||
| 	'var(--theme-chart-data-2, #2ca02c)', | 	'var(--theme-chart-shape-yellow-fill, #fffec6)', | ||||||
| 	'var(--theme-chart-data-3, #d62728)', | 	'var(--theme-chart-shape-green-fill, #d6f9d5)', | ||||||
| 	'var(--theme-chart-data-4, #9467bd)', | 	'var(--theme-chart-shape-teal-fill, #b0ebe9)', | ||||||
| 	'var(--theme-chart-data-5, #8c564b)', | 	'var(--theme-chart-shape-pink-fill, #ffcae2)', | ||||||
| 	'var(--theme-chart-data-6, #e377c2)', | 	'var(--theme-chart-shape-purple-fill, #efdeff)', | ||||||
| 	'var(--theme-chart-data-7, #7f7f7f)', | 	'var(--theme-chart-shape-blue-fill, #caebff)', | ||||||
| 	'var(--theme-chart-data-8, #bcbd22)', | 	'var(--theme-chart-shape-indigo-fill, #c2d6f9)', | ||||||
| 	'var(--theme-chart-data-9, #17becf)', | 	'var(--theme-chart-shape-magenta-fill, #ebb5cd)', | ||||||
|  | 	'var(--theme-chart-shape-brown-fill, #e1c3b8)', | ||||||
|  |   	'var(--theme-chart-shape-red-line, #ff806d)', | ||||||
|  |   	'var(--theme-chart-shape-orange-line, #ffb780)', | ||||||
|  |   	'var(--theme-chart-shape-yellow-line, #e1dc18)', | ||||||
|  |   	'var(--theme-chart-shape-green-line, #66cc66)', | ||||||
|  |   	'var(--theme-chart-shape-teal-line, #66cdcc)', | ||||||
|  |   	'var(--theme-chart-shape-pink-line, #ff99cb)', | ||||||
|  |   	'var(--theme-chart-shape-purple-line, #cc99fe)', | ||||||
|  |   	'var(--theme-chart-shape-blue-line, #66cbff)', | ||||||
|  |   	'var(--theme-chart-shape-indigo-line, #6295ee)', | ||||||
|  |   	'var(--theme-chart-shape-magenta-line, #cc6698)', | ||||||
|  |   	'var(--theme-chart-shape-brown-line, #bf8c71)', | ||||||
| ]; | ]; | ||||||
|  |  | ||||||
| export function* chart_data_color_generator() : Generator<string, never> { | export function* chart_data_color_generator() : Generator<string, never> { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user