@github/paste-markdown
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -0,1 +1,2 @@ | ||
import { install as installHTML, uninstall as uninstallHTML } from './paste-markdown-html'; | ||
import { install as installImageLink, uninstall as uninstallImageLink } from './paste-markdown-image-link'; | ||
@@ -9,2 +10,2 @@ import { install as installLink, uninstall as uninstallLink } from './paste-markdown-link'; | ||
declare function subscribe(el: HTMLElement): Subscription; | ||
export { subscribe, installImageLink, installLink, installTable, installText, uninstallImageLink, uninstallTable, uninstallLink, uninstallText }; | ||
export { subscribe, installHTML, installImageLink, installLink, installTable, installText, uninstallHTML, uninstallImageLink, uninstallTable, uninstallLink, uninstallText }; |
@@ -33,2 +33,60 @@ function insertText(textarea, text) { | ||
function install$4(el) { | ||
el.addEventListener('paste', onPaste$4); | ||
} | ||
function uninstall$4(el) { | ||
el.removeEventListener('paste', onPaste$4); | ||
} | ||
function onPaste$4(event) { | ||
const transfer = event.clipboardData; | ||
if (!transfer || !hasHTML(transfer)) | ||
return; | ||
const field = event.currentTarget; | ||
if (!(field instanceof HTMLTextAreaElement)) | ||
return; | ||
let text = transfer.getData('text/plain'); | ||
const textHTML = transfer.getData('text/html'); | ||
if (!textHTML) | ||
return; | ||
text = text.trim(); | ||
if (!text) | ||
return; | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(textHTML, 'text/html'); | ||
const a = doc.getElementsByTagName('a'); | ||
const markdown = transform(a, text, linkify$2); | ||
if (markdown === text) | ||
return; | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
insertText(field, markdown); | ||
} | ||
function transform(elements, text, transformer, ...args) { | ||
const markdownParts = []; | ||
for (const element of elements) { | ||
const textContent = element.textContent || ''; | ||
const { part, index } = trimAfter(text, textContent); | ||
markdownParts.push(part.replace(textContent, transformer(element, args))); | ||
text = text.slice(index); | ||
} | ||
markdownParts.push(text); | ||
return markdownParts.join(''); | ||
} | ||
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 hasHTML(transfer) { | ||
return transfer.types.includes('text/html'); | ||
} | ||
function linkify$2(element) { | ||
return `[${element.textContent}](${element.href})`; | ||
} | ||
function install$3(el) { | ||
@@ -262,5 +320,7 @@ el.addEventListener('dragover', onDragover$1); | ||
install(el); | ||
install$4(el); | ||
return { | ||
unsubscribe: () => { | ||
uninstall$1(el); | ||
uninstall$4(el); | ||
uninstall$3(el); | ||
@@ -273,2 +333,2 @@ uninstall$2(el); | ||
export { install$3 as installImageLink, install$2 as installLink, install$1 as installTable, install as installText, subscribe, uninstall$3 as uninstallImageLink, uninstall$2 as uninstallLink, uninstall$1 as uninstallTable, uninstall as uninstallText }; | ||
export { install$4 as installHTML, install$3 as installImageLink, install$2 as installLink, install$1 as installTable, install as installText, subscribe, uninstall$4 as uninstallHTML, uninstall$3 as uninstallImageLink, uninstall$2 as uninstallLink, uninstall$1 as uninstallTable, uninstall as uninstallText }; |
@@ -39,2 +39,60 @@ (function (global, factory) { | ||
function install$4(el) { | ||
el.addEventListener('paste', onPaste$4); | ||
} | ||
function uninstall$4(el) { | ||
el.removeEventListener('paste', onPaste$4); | ||
} | ||
function onPaste$4(event) { | ||
const transfer = event.clipboardData; | ||
if (!transfer || !hasHTML(transfer)) | ||
return; | ||
const field = event.currentTarget; | ||
if (!(field instanceof HTMLTextAreaElement)) | ||
return; | ||
let text = transfer.getData('text/plain'); | ||
const textHTML = transfer.getData('text/html'); | ||
if (!textHTML) | ||
return; | ||
text = text.trim(); | ||
if (!text) | ||
return; | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(textHTML, 'text/html'); | ||
const a = doc.getElementsByTagName('a'); | ||
const markdown = transform(a, text, linkify$2); | ||
if (markdown === text) | ||
return; | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
insertText(field, markdown); | ||
} | ||
function transform(elements, text, transformer, ...args) { | ||
const markdownParts = []; | ||
for (const element of elements) { | ||
const textContent = element.textContent || ''; | ||
const { part, index } = trimAfter(text, textContent); | ||
markdownParts.push(part.replace(textContent, transformer(element, args))); | ||
text = text.slice(index); | ||
} | ||
markdownParts.push(text); | ||
return markdownParts.join(''); | ||
} | ||
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 hasHTML(transfer) { | ||
return transfer.types.includes('text/html'); | ||
} | ||
function linkify$2(element) { | ||
return `[${element.textContent}](${element.href})`; | ||
} | ||
function install$3(el) { | ||
@@ -268,5 +326,7 @@ el.addEventListener('dragover', onDragover$1); | ||
install(el); | ||
install$4(el); | ||
return { | ||
unsubscribe: () => { | ||
uninstall$1(el); | ||
uninstall$4(el); | ||
uninstall$3(el); | ||
@@ -279,2 +339,3 @@ uninstall$2(el); | ||
exports.installHTML = install$4; | ||
exports.installImageLink = install$3; | ||
@@ -285,2 +346,3 @@ exports.installLink = install$2; | ||
exports.subscribe = subscribe; | ||
exports.uninstallHTML = uninstall$4; | ||
exports.uninstallImageLink = uninstall$3; | ||
@@ -287,0 +349,0 @@ exports.uninstallLink = uninstall$2; |
{ | ||
"name": "@github/paste-markdown", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Paste spreadsheet cells as a Markdown table.", | ||
@@ -5,0 +5,0 @@ "repository": "github/paste-markdown", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27380
12
687