mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	feat(md): rewrite image URL with spaces
This commit is contained in:
		
							parent
							
								
									f67e2ebcd5
								
							
						
					
					
						commit
						a441ea2461
					
				| @ -98,4 +98,10 @@ describe("Markdown export", () => { | ||||
|             ###### Heading 6`;
 | ||||
|         expect(markdownExportService.toMarkdown(html)).toBe(expected); | ||||
|     }); | ||||
| 
 | ||||
|     it("rewrites image URL with spaces", () => { | ||||
|         const html = `<img src="Hello world  .png"/>`; | ||||
|         const expected = ``; | ||||
|         expect(markdownExportService.toMarkdown(html)).toBe(expected); | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| @ -30,6 +30,7 @@ function toMarkdown(content: string) { | ||||
|         }); | ||||
|         // Filter is heavily based on: https://github.com/mixmark-io/turndown/issues/274#issuecomment-458730974
 | ||||
|         instance.addRule("fencedCodeBlock", fencedCodeBlockFilter); | ||||
|         instance.addRule("img", buildImageFilter()); | ||||
|         instance.use(turndownPluginGfm.gfm); | ||||
|     } | ||||
| 
 | ||||
| @ -52,6 +53,50 @@ function rewriteLanguageTag(source: string) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // TODO: Remove once upstream delivers a fix for https://github.com/mixmark-io/turndown/issues/467.
 | ||||
| function buildImageFilter() { | ||||
|     const ESCAPE_PATTERNS = { | ||||
|         before: /([\\*`[\]_]|(?:^[-+>])|(?:^~~~)|(?:^#{1-6}))/g, | ||||
|         after: /((?:^\d+(?=\.)))/ | ||||
|     } | ||||
| 
 | ||||
|     const escapePattern = new RegExp('(?:' + ESCAPE_PATTERNS.before.source + '|' + ESCAPE_PATTERNS.after.source + ')', 'g'); | ||||
| 
 | ||||
|     function escapeMarkdown (content: string) { | ||||
|         return content.replace(escapePattern, function (match, before, after) { | ||||
|             return before ? '\\' + before : after + '\\' | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     function escapeLinkDestination(destination: string) { | ||||
|         return destination | ||||
|             .replace(/([()])/g, '\\$1') | ||||
|             .replace(/ /g, "%20"); | ||||
|     } | ||||
| 
 | ||||
|     function escapeLinkTitle (title: string) { | ||||
|         return title.replace(/"/g, '\\"') | ||||
|     } | ||||
| 
 | ||||
|     function cleanAttribute (attribute: string) { | ||||
|         return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : '' | ||||
|     } | ||||
| 
 | ||||
|     const imageFilter: TurndownService.Rule = { | ||||
|         filter: "img", | ||||
|         replacement(content, node) { | ||||
|             const untypedNode = (node as any); | ||||
|             const alt = escapeMarkdown(cleanAttribute(untypedNode.getAttribute('alt'))) | ||||
|             const src = escapeLinkDestination(untypedNode.getAttribute('src') || '') | ||||
|             const title = cleanAttribute(untypedNode.getAttribute('title')) | ||||
|             const titlePart = title ? ' "' + escapeLinkTitle(title) + '"' : '' | ||||
| 
 | ||||
|             return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : '' | ||||
|         } | ||||
|     }; | ||||
|     return imageFilter; | ||||
| } | ||||
| 
 | ||||
| export default { | ||||
|     toMarkdown | ||||
| }; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran