You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	Clipper: Fixed issue with relative links when importing HTML
This commit is contained in:
		| @@ -1,5 +1,13 @@ | |||||||
| const jsdom = require("jsdom"); | const jsdom = require("jsdom"); | ||||||
| const { JSDOM } = jsdom; | const { JSDOM } = jsdom; | ||||||
|  | const urlUtils = require('lib/urlUtils.js'); | ||||||
|  |  | ||||||
|  | function headAndBodyHtml_(doc) { | ||||||
|  | 	const output = []; | ||||||
|  | 	if (doc.head) output.push(doc.head.innerHTML); | ||||||
|  | 	if (doc.body) output.push(doc.body.innerHTML); | ||||||
|  | 	return output.join('\n'); | ||||||
|  | } | ||||||
|  |  | ||||||
| const htmlUtils = { | const htmlUtils = { | ||||||
|  |  | ||||||
| @@ -34,11 +42,22 @@ const htmlUtils = { | |||||||
|  |  | ||||||
| 		// This function returns the head and body but without the <head> and <body> | 		// This function returns the head and body but without the <head> and <body> | ||||||
| 		// tag, which for our purpose are not needed and can cause issues when present. | 		// tag, which for our purpose are not needed and can cause issues when present. | ||||||
| 		const output = []; | 		return headAndBodyHtml_(doc); | ||||||
| 		if (doc.head) output.push(doc.head.innerHTML); | 	}, | ||||||
| 		if (doc.body) output.push(doc.body.innerHTML); |  | ||||||
| 		return output.join('\n'); | 	prependBaseUrl(html, baseUrl) { | ||||||
| 	},   | 		const dom = new JSDOM(html); | ||||||
|  | 		const doc = dom.window.document; | ||||||
|  | 		const anchors = doc.getElementsByTagName('a'); | ||||||
|  |  | ||||||
|  | 		for (const anchor of anchors) { | ||||||
|  | 			const href = anchor.getAttribute('href'); | ||||||
|  | 			const newHref = urlUtils.prependBaseUrl(href, baseUrl); | ||||||
|  | 			anchor.setAttribute('href', newHref); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		return headAndBodyHtml_(doc); | ||||||
|  | 	},  | ||||||
|  |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -432,6 +432,8 @@ class Api { | |||||||
|  |  | ||||||
| 		if (requestNote.id) output.id = requestNote.id; | 		if (requestNote.id) output.id = requestNote.id; | ||||||
|  |  | ||||||
|  | 		const baseUrl = requestNote.base_url ? requestNote.base_url : ''; | ||||||
|  |  | ||||||
| 		if (requestNote.body_html) { | 		if (requestNote.body_html) { | ||||||
| 			if (requestNote.convert_to === 'html') { | 			if (requestNote.convert_to === 'html') { | ||||||
| 				const style = await this.buildNoteStyleSheet_(requestNote.stylesheets); | 				const style = await this.buildNoteStyleSheet_(requestNote.stylesheets); | ||||||
| @@ -446,13 +448,14 @@ class Api { | |||||||
| 					// means a code block in Markdown. | 					// means a code block in Markdown. | ||||||
| 					collapseWhitespace: true, | 					collapseWhitespace: true, | ||||||
| 				}); | 				}); | ||||||
|  | 				output.body = htmlUtils.prependBaseUrl(output.body, baseUrl); | ||||||
| 				output.markup_language = Note.MARKUP_LANGUAGE_HTML; | 				output.markup_language = Note.MARKUP_LANGUAGE_HTML; | ||||||
| 			} else { // Convert to Markdown  | 			} else { // Convert to Markdown  | ||||||
| 				// Parsing will not work if the HTML is not wrapped in a top level tag, which is not guaranteed | 				// Parsing will not work if the HTML is not wrapped in a top level tag, which is not guaranteed | ||||||
| 				// when getting the content from elsewhere. So here wrap it - it won't change anything to the final | 				// when getting the content from elsewhere. So here wrap it - it won't change anything to the final | ||||||
| 				// rendering but it makes sure everything will be parsed. | 				// rendering but it makes sure everything will be parsed. | ||||||
| 				output.body = await this.htmlToMdParser().parse('<div>' + requestNote.body_html + '</div>', { | 				output.body = await this.htmlToMdParser().parse('<div>' + requestNote.body_html + '</div>', { | ||||||
| 					baseUrl: requestNote.base_url ? requestNote.base_url : '', | 					baseUrl: baseUrl, | ||||||
| 					anchorNames: requestNote.anchor_names ? requestNote.anchor_names : [], | 					anchorNames: requestNote.anchor_names ? requestNote.anchor_names : [], | ||||||
| 				}); | 				}); | ||||||
| 				output.markup_language = Note.MARKUP_LANGUAGE_MARKDOWN; | 				output.markup_language = Note.MARKUP_LANGUAGE_MARKDOWN; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user