mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	save and remove both hyper and image links
This commit is contained in:
		| @@ -169,25 +169,55 @@ async function protectNoteRevisions(note) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function findImageLinks(content, foundLinks) { | ||||||
|  |     const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g; | ||||||
|  |     let match; | ||||||
|  |  | ||||||
|  |     while (match = re.exec(content)) { | ||||||
|  |         foundLinks.push({ | ||||||
|  |             type: 'image', | ||||||
|  |             targetNoteId: match[1] | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  |     return match; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function findHyperLinks(content, foundLinks) { | ||||||
|  |     const re = /href="#root[a-zA-Z0-9\/]*\/([a-zA-Z0-9]+)\//g; | ||||||
|  |     let match; | ||||||
|  |  | ||||||
|  |     while (match = re.exec(content)) { | ||||||
|  |         foundLinks.push({ | ||||||
|  |             type: 'hyper', | ||||||
|  |             targetNoteId: match[1] | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return match; | ||||||
|  | } | ||||||
|  |  | ||||||
| async function saveLinks(note) { | async function saveLinks(note) { | ||||||
|     if (note.type !== 'text') { |     if (note.type !== 'text') { | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const existingLinks = await note.getLinks(); |     const foundLinks = []; | ||||||
|     const foundNoteIds = []; |  | ||||||
|     const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g; |  | ||||||
|     let match; |  | ||||||
|  |  | ||||||
|     while (match = re.exec(note.content)) { |     findImageLinks(note.content, foundLinks); | ||||||
|         const targetNoteId = match[1]; |     findHyperLinks(note.content, foundLinks); | ||||||
|         const existingLink = existingLinks.find(link => link.targetNoteId === targetNoteId && link.type === 'image'); |  | ||||||
|  |     const existingLinks = await note.getLinks(); | ||||||
|  |  | ||||||
|  |     for (const foundLink of foundLinks) { | ||||||
|  |         const existingLink = existingLinks.find(existingLink => | ||||||
|  |             existingLink.targetNoteId === foundLink.targetNoteId | ||||||
|  |             && existingLink.type === foundLink.type); | ||||||
|  |  | ||||||
|         if (!existingLink) { |         if (!existingLink) { | ||||||
|             await new Link({ |             await new Link({ | ||||||
|                 noteId: note.noteId, |                 noteId: note.noteId, | ||||||
|                 targetNoteId, |                 targetNoteId: foundLink.targetNoteId, | ||||||
|                 type: 'image' |                 type: foundLink.type | ||||||
|             }).save(); |             }).save(); | ||||||
|         } |         } | ||||||
|         else if (existingLink.isDeleted) { |         else if (existingLink.isDeleted) { | ||||||
| @@ -195,12 +225,12 @@ async function saveLinks(note) { | |||||||
|             await existingLink.save(); |             await existingLink.save(); | ||||||
|         } |         } | ||||||
|         // else the link exists so we don't need to do anything |         // else the link exists so we don't need to do anything | ||||||
|  |  | ||||||
|         foundNoteIds.push(targetNoteId); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // marking links as deleted if they are not present on the page anymore |     // marking links as deleted if they are not present on the page anymore | ||||||
|     const unusedLinks = existingLinks.filter(link => !foundNoteIds.includes(link.noteId)); |     const unusedLinks = existingLinks.filter(existingLink => !foundLinks.some(foundLink => | ||||||
|  |                                     existingLink.targetNoteId === foundLink.targetNoteId | ||||||
|  |                                     && existingLink.type === foundLink.type)); | ||||||
|  |  | ||||||
|     for (const unusedLink of unusedLinks) { |     for (const unusedLink of unusedLinks) { | ||||||
|         unusedLink.isDeleted = true; |         unusedLink.isDeleted = true; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user