mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	feat(import/markdown): collapse unnecessary whitespace when importing
This commit is contained in:
		
							parent
							
								
									c892c95aae
								
							
						
					
					
						commit
						a2f0896c2a
					
				| @ -56,4 +56,22 @@ describe("markdown", () => { | ||||
|         `, "What's new")
 | ||||
|         expect(result).toBe(`\n<p>Hi there</p>\n`); | ||||
|     }); | ||||
| 
 | ||||
|     it("trims unnecessary whitespace", () => { | ||||
|         const input = `\ | ||||
| ## Heading 1 | ||||
| 
 | ||||
| Title | ||||
| 
 | ||||
| \`\`\` | ||||
| code block 1 | ||||
| second line 2 | ||||
| \`\`\` | ||||
| `;
 | ||||
|         const expected = `\ | ||||
| <h2>Heading 1</h2><p>Title</p><pre><code class="language-text-x-trilium-auto">code block 1 | ||||
| second line 2</code></pre>`;
 | ||||
|         expect(markdownService.renderToHtml(input, "Troubleshooting")).toBe(expected); | ||||
|     }); | ||||
| 
 | ||||
| }); | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| "use strict"; | ||||
| 
 | ||||
| import { parse, Renderer, type Tokens } from "marked"; | ||||
| import { minify as minifyHtml } from "html-minifier"; | ||||
| 
 | ||||
| const renderer = new Renderer({ async: false }); | ||||
| renderer.code = ({ text, lang, escaped }: Tokens.Code) => { | ||||
| @ -17,12 +18,19 @@ import importUtils from "./utils.js"; | ||||
| import { getMimeTypeFromHighlightJs, MIME_TYPE_AUTO, normalizeMimeTypeForCKEditor } from "./mime_type_definitions.js"; | ||||
| 
 | ||||
| function renderToHtml(content: string, title: string) { | ||||
|     const html = parse(content, { | ||||
|     let html = parse(content, { | ||||
|         async: false, | ||||
|         renderer: renderer | ||||
|     }) as string; | ||||
|     const h1Handled = importUtils.handleH1(html, title); // h1 handling needs to come before sanitization
 | ||||
|     return htmlSanitizer.sanitize(h1Handled); | ||||
| 
 | ||||
|     // h1 handling needs to come before sanitization
 | ||||
|     html = importUtils.handleH1(html, title); | ||||
|     html = htmlSanitizer.sanitize(html); | ||||
|     html = minifyHtml(html, { | ||||
|         collapseWhitespace: true | ||||
|     }); | ||||
| 
 | ||||
|     return html; | ||||
| } | ||||
| 
 | ||||
| function getNormalizedMimeFromMarkdownLanguage(language: string | undefined) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran