@github/paste-markdown
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -46,15 +46,15 @@ function insertText(textarea, text) { | ||
return; | ||
let text = transfer.getData('text/plain'); | ||
let plaintext = transfer.getData('text/plain'); | ||
const textHTML = transfer.getData('text/html'); | ||
const textHTMLClean = textHTML.replace(/\u00A0/g, ' '); | ||
const textHTMLClean = textHTML.replace(/\u00A0/g, ' ').replace(/\uC2A0/g, ' '); | ||
if (!textHTML) | ||
return; | ||
text = text.trim(); | ||
if (!text) | ||
plaintext = plaintext.trim(); | ||
if (!plaintext) | ||
return; | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(textHTMLClean, 'text/html'); | ||
const a = doc.getElementsByTagName('a'); | ||
const markdown = transform(a, text, linkify$2); | ||
if (markdown === text) | ||
const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT); | ||
const markdown = convertToMarkdown(plaintext, walker); | ||
if (markdown === plaintext) | ||
return; | ||
@@ -65,25 +65,39 @@ event.stopPropagation(); | ||
} | ||
function transform(elements, text, transformer, ...args) { | ||
const markdownParts = []; | ||
for (const element of elements) { | ||
const textContent = element.textContent || ''; | ||
const { part, index } = trimAfter(text, textContent); | ||
if (index >= 0) { | ||
markdownParts.push(part.replace(textContent, transformer(element, args))); | ||
text = text.slice(index); | ||
function convertToMarkdown(plaintext, walker) { | ||
var _a; | ||
let currentNode = walker.firstChild(); | ||
let markdown = plaintext; | ||
let markdownIgnoreBeforeIndex = 0; | ||
let index = 0; | ||
const NODE_LIMIT = 10000; | ||
while (currentNode && index < NODE_LIMIT) { | ||
index++; | ||
const text = isLink(currentNode) ? currentNode.textContent || '' : ((_a = currentNode.firstChild) === null || _a === void 0 ? void 0 : _a.wholeText) || ''; | ||
if (isEmptyString(text)) { | ||
currentNode = walker.nextNode(); | ||
continue; | ||
} | ||
const markdownFoundIndex = markdown.indexOf(text, markdownIgnoreBeforeIndex); | ||
if (markdownFoundIndex >= 0) { | ||
if (isLink(currentNode)) { | ||
const markdownLink = linkify$2(currentNode); | ||
markdown = | ||
markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length); | ||
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length; | ||
} | ||
else { | ||
markdownIgnoreBeforeIndex = markdownFoundIndex + text.length; | ||
} | ||
} | ||
currentNode = walker.nextNode(); | ||
} | ||
markdownParts.push(text); | ||
return markdownParts.join(''); | ||
return index === NODE_LIMIT ? plaintext : markdown; | ||
} | ||
function trimAfter(text, search = '') { | ||
let index = text.indexOf(search); | ||
if (index === -1) | ||
return { part: '', index }; | ||
index += search.length; | ||
return { | ||
part: text.substring(0, index), | ||
index | ||
}; | ||
function isEmptyString(text) { | ||
return !text || (text === null || text === void 0 ? void 0 : text.trim().length) === 0; | ||
} | ||
function isLink(node) { | ||
var _a; | ||
return ((_a = node.tagName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'a' && node.hasAttribute('href'); | ||
} | ||
function hasHTML(transfer) { | ||
@@ -90,0 +104,0 @@ return transfer.types.includes('text/html'); |
@@ -52,15 +52,15 @@ (function (global, factory) { | ||
return; | ||
let text = transfer.getData('text/plain'); | ||
let plaintext = transfer.getData('text/plain'); | ||
const textHTML = transfer.getData('text/html'); | ||
const textHTMLClean = textHTML.replace(/\u00A0/g, ' '); | ||
const textHTMLClean = textHTML.replace(/\u00A0/g, ' ').replace(/\uC2A0/g, ' '); | ||
if (!textHTML) | ||
return; | ||
text = text.trim(); | ||
if (!text) | ||
plaintext = plaintext.trim(); | ||
if (!plaintext) | ||
return; | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(textHTMLClean, 'text/html'); | ||
const a = doc.getElementsByTagName('a'); | ||
const markdown = transform(a, text, linkify$2); | ||
if (markdown === text) | ||
const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT); | ||
const markdown = convertToMarkdown(plaintext, walker); | ||
if (markdown === plaintext) | ||
return; | ||
@@ -71,25 +71,39 @@ event.stopPropagation(); | ||
} | ||
function transform(elements, text, transformer, ...args) { | ||
const markdownParts = []; | ||
for (const element of elements) { | ||
const textContent = element.textContent || ''; | ||
const { part, index } = trimAfter(text, textContent); | ||
if (index >= 0) { | ||
markdownParts.push(part.replace(textContent, transformer(element, args))); | ||
text = text.slice(index); | ||
function convertToMarkdown(plaintext, walker) { | ||
var _a; | ||
let currentNode = walker.firstChild(); | ||
let markdown = plaintext; | ||
let markdownIgnoreBeforeIndex = 0; | ||
let index = 0; | ||
const NODE_LIMIT = 10000; | ||
while (currentNode && index < NODE_LIMIT) { | ||
index++; | ||
const text = isLink(currentNode) ? currentNode.textContent || '' : ((_a = currentNode.firstChild) === null || _a === void 0 ? void 0 : _a.wholeText) || ''; | ||
if (isEmptyString(text)) { | ||
currentNode = walker.nextNode(); | ||
continue; | ||
} | ||
const markdownFoundIndex = markdown.indexOf(text, markdownIgnoreBeforeIndex); | ||
if (markdownFoundIndex >= 0) { | ||
if (isLink(currentNode)) { | ||
const markdownLink = linkify$2(currentNode); | ||
markdown = | ||
markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length); | ||
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length; | ||
} | ||
else { | ||
markdownIgnoreBeforeIndex = markdownFoundIndex + text.length; | ||
} | ||
} | ||
currentNode = walker.nextNode(); | ||
} | ||
markdownParts.push(text); | ||
return markdownParts.join(''); | ||
return index === NODE_LIMIT ? plaintext : markdown; | ||
} | ||
function trimAfter(text, search = '') { | ||
let index = text.indexOf(search); | ||
if (index === -1) | ||
return { part: '', index }; | ||
index += search.length; | ||
return { | ||
part: text.substring(0, index), | ||
index | ||
}; | ||
function isEmptyString(text) { | ||
return !text || (text === null || text === void 0 ? void 0 : text.trim().length) === 0; | ||
} | ||
function isLink(node) { | ||
var _a; | ||
return ((_a = node.tagName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'a' && node.hasAttribute('href'); | ||
} | ||
function hasHTML(transfer) { | ||
@@ -96,0 +110,0 @@ return transfer.types.includes('text/html'); |
{ | ||
"name": "@github/paste-markdown", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "Paste spreadsheet cells as a Markdown table.", | ||
@@ -30,2 +30,3 @@ "repository": "github/paste-markdown", | ||
"chai": "^4.3.4", | ||
"chromium": "^3.0.3", | ||
"eslint": "^7.32.0", | ||
@@ -32,0 +33,0 @@ "eslint-plugin-github": "^4.3.0", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
31440
771
0
14