kopia lustrzana https://github.com/cheeaun/phanpy
				
				
				
			
		
			
				
	
	
		
			83 wiersze
		
	
	
		
			2.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
			
		
		
	
	
			83 wiersze
		
	
	
		
			2.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
| import emojifyText from './emojify-text';
 | |
| 
 | |
| const fauxDiv = document.createElement('div');
 | |
| 
 | |
| export default (content, opts = {}) => {
 | |
|   const { emojis, postEnhanceDOM = () => {} } = opts;
 | |
|   let enhancedContent = content;
 | |
|   const dom = document.createElement('div');
 | |
|   dom.innerHTML = enhancedContent;
 | |
| 
 | |
|   // Add target="_blank" to all links with no target="_blank"
 | |
|   // E.g. `note` in `account`
 | |
|   const links = Array.from(dom.querySelectorAll('a:not([target="_blank"])'));
 | |
|   links.forEach((link) => {
 | |
|     link.setAttribute('target', '_blank');
 | |
|   });
 | |
| 
 | |
|   // EMOJIS
 | |
|   // ======
 | |
|   // Convert :shortcode: to <img />
 | |
|   let textNodes = extractTextNodes(dom);
 | |
|   textNodes.forEach((node) => {
 | |
|     let html = node.nodeValue;
 | |
|     if (emojis) {
 | |
|       html = emojifyText(html, emojis);
 | |
|     }
 | |
|     fauxDiv.innerHTML = html;
 | |
|     const nodes = Array.from(fauxDiv.childNodes);
 | |
|     node.replaceWith(...nodes);
 | |
|   });
 | |
| 
 | |
|   // INLINE CODE
 | |
|   // ===========
 | |
|   // Convert `code` to <code>code</code>
 | |
|   textNodes = extractTextNodes(dom);
 | |
|   textNodes.forEach((node) => {
 | |
|     let html = node.nodeValue;
 | |
|     if (/`[^`]+`/g.test(html)) {
 | |
|       html = html.replaceAll(/(`[^]+?`)/g, '<code>$1</code>');
 | |
|     }
 | |
|     fauxDiv.innerHTML = html;
 | |
|     const nodes = Array.from(fauxDiv.childNodes);
 | |
|     node.replaceWith(...nodes);
 | |
|   });
 | |
| 
 | |
|   // CODE BLOCKS
 | |
|   // ===========
 | |
|   // Convert ```code``` to <pre><code>code</code></pre>
 | |
|   const blocks = Array.from(dom.querySelectorAll('p')).filter((p) =>
 | |
|     /^```[^]+```$/g.test(p.innerText.trim()),
 | |
|   );
 | |
|   blocks.forEach((block) => {
 | |
|     const pre = document.createElement('pre');
 | |
|     // Replace <br /> with newlines
 | |
|     block.querySelectorAll('br').forEach((br) => br.replaceWith('\n'));
 | |
|     pre.innerHTML = `<code>${block.innerText.trim()}</code>`;
 | |
|     block.replaceWith(pre);
 | |
|   });
 | |
| 
 | |
|   if (postEnhanceDOM) {
 | |
|     postEnhanceDOM(dom); // mutate dom
 | |
|   }
 | |
| 
 | |
|   enhancedContent = dom.innerHTML;
 | |
| 
 | |
|   return enhancedContent;
 | |
| };
 | |
| 
 | |
| function extractTextNodes(dom) {
 | |
|   const textNodes = [];
 | |
|   const walk = document.createTreeWalker(
 | |
|     dom,
 | |
|     NodeFilter.SHOW_TEXT,
 | |
|     null,
 | |
|     false,
 | |
|   );
 | |
|   let node;
 | |
|   while ((node = walk.nextNode())) {
 | |
|     textNodes.push(node);
 | |
|   }
 | |
|   return textNodes;
 | |
| }
 |