| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | function textNode(text) { | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	const node = new commonmark.Node('text', undefined); | 
					
						
							|  |  |  | 	node.literal = text; | 
					
						
							|  |  |  | 	return node; | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function linkNode(text, link) { | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	const linkNode = new commonmark.Node('link', undefined); | 
					
						
							|  |  |  | 	if (link.startsWith('#')) { | 
					
						
							| 
									
										
										
										
											2025-01-04 13:03:58 -05:00
										 |  |  | 		linkNode.destination = `#${encodeURIComponent(link)}`; | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		linkNode.destination = link; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	linkNode.appendChild(textNode(text)); | 
					
						
							|  |  |  | 	return linkNode; | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function splitMatches(text, regexp) { | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	// Regexp must be sticky.
 | 
					
						
							|  |  |  | 	regexp = new RegExp(regexp, 'gm'); | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	let i = 0; | 
					
						
							|  |  |  | 	const result = []; | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	let match = regexp.exec(text); | 
					
						
							|  |  |  | 	while (match) { | 
					
						
							|  |  |  | 		const matchText = match[0]; | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 		if (match.index > i) { | 
					
						
							|  |  |  | 			result.push([text.substring(i, match.index), false]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 		result.push([matchText, true]); | 
					
						
							|  |  |  | 		i = match.index + matchText.length; | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 		match = regexp.exec(text); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	if (i < text.length) { | 
					
						
							|  |  |  | 		result.push([text.substring(i, text.length), false]); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	return result; | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | const regex = new RegExp('(?:https?://[^ ]+[^ .,])|(?:(?<!\\w)#[\\w-]+)|(?:@[A-Za-z0-9+/]+=.ed25519)|(?:[%&][A-Za-z0-9+/]+=.sha256)'); | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function split(textNodes) { | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	const text = textNodes.map((n) => n.literal).join(''); | 
					
						
							|  |  |  | 	const parts = splitMatches(text, regex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return parts.map((part) => { | 
					
						
							|  |  |  | 		if (part[1]) { | 
					
						
							|  |  |  | 			return linkNode(part[0], part[0]); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return textNode(part[0]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function transform(parsed) { | 
					
						
							| 
									
										
										
										
											2024-04-04 00:18:39 +01:00
										 |  |  | 	const walker = parsed.walker(); | 
					
						
							|  |  |  | 	let event; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let nodes = []; | 
					
						
							|  |  |  | 	while ((event = walker.next())) { | 
					
						
							|  |  |  | 		const node = event.node; | 
					
						
							|  |  |  | 		if (event.entering && node.type === 'text') { | 
					
						
							|  |  |  | 			nodes.push(node); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if (nodes.length > 0) { | 
					
						
							|  |  |  | 				split(nodes) | 
					
						
							|  |  |  | 					.reverse() | 
					
						
							|  |  |  | 					.forEach((newNode) => { | 
					
						
							|  |  |  | 						nodes[0].insertAfter(newNode); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				nodes.forEach((n) => n.unlink()); | 
					
						
							|  |  |  | 				nodes = []; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (nodes.length > 0) { | 
					
						
							|  |  |  | 		split(nodes) | 
					
						
							|  |  |  | 			.reverse() | 
					
						
							|  |  |  | 			.forEach((newNode) => { | 
					
						
							|  |  |  | 				nodes[0].insertAfter(newNode); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		nodes.forEach((n) => n.unlink()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return parsed; | 
					
						
							| 
									
										
										
										
											2022-11-12 03:06:29 +00:00
										 |  |  | } |