Skip to content

Commit

Permalink
Merge pull request #7 from github/copy-fixes
Browse files Browse the repository at this point in the history
Fixes for copying and quoting Markdown
  • Loading branch information
mislav authored Feb 5, 2019
2 parents dc1209a + 327dc52 commit 9228503
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
26 changes: 23 additions & 3 deletions markdown-parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function isCheckbox(node: Node): boolean {
return node.nodeName === 'INPUT' && node instanceof HTMLInputElement && node.type === 'checkbox'
}

function isHighlightContainer(el: HTMLElement): boolean {
return el.nodeName === 'DIV' && el.classList.contains('highlight')
}

let listIndexOffset = 0

function nestedListExclusive(li: Element): boolean {
Expand Down Expand Up @@ -79,7 +83,7 @@ const filters: {[key: string]: (HTMLElement) => string | HTMLElement} = {
},
PRE(el) {
const parent = el.parentNode
if (parent instanceof HTMLElement && parent.nodeName === 'DIV' && parent.classList.contains('highlight')) {
if (parent instanceof HTMLElement && isHighlightContainer(parent)) {
const match = parent.className.match(/highlight-source-(\S+)/)
const flavor = match ? match[1] : ''
const text = el.textContent.replace(/\n+$/, '')
Expand Down Expand Up @@ -212,7 +216,7 @@ function fragmentToMarkdown(
}
}

export default function rangeToMarkdown(range: Range, selector?: string): DocumentFragment {
export default function rangeToMarkdown(range: Range, selector: string, unwrap: boolean): DocumentFragment {
const startNode = range.startContainer
if (!startNode || !startNode.parentNode || !(startNode.parentNode instanceof HTMLElement)) {
throw new Error('the range must start within an HTMLElement')
Expand All @@ -230,7 +234,23 @@ export default function rangeToMarkdown(range: Range, selector?: string): Docume

listIndexOffset = 0
const li = parent.closest('li')
if (li && li.parentNode) {
const codeBlock = parent.closest('pre')
if (codeBlock) {
const pre = document.createElement('pre')
pre.appendChild(fragment)
let item = pre
if (!unwrap) {
const pp = codeBlock.parentNode
if (pp instanceof HTMLElement && isHighlightContainer(pp)) {
const div = document.createElement('div')
div.className = pp.className
div.appendChild(item)
item = div
}
}
fragment = document.createDocumentFragment()
fragment.appendChild(item)
} else if (li && li.parentNode) {
if (li.parentNode.nodeName === 'OL') {
listIndexOffset = indexInList(li)
}
Expand Down
11 changes: 7 additions & 4 deletions quote-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ function onCopy(event: ClipboardEvent) {
} catch (err) {
return
}
const quoted = extractQuote(selection.toString(), range)
const quoted = extractQuote(selection.toString(), range, true)
if (!quoted) return

transfer.setData('text/plain', quoted.selectionText)
event.preventDefault()

selection.removeAllRanges()
selection.addRange(range)
}

function eventIsNotRelevant(event: KeyboardEvent): boolean {
Expand Down Expand Up @@ -96,7 +99,7 @@ function quoteSelection(event: KeyboardEvent): void {
}

export function quote(text: string, range: Range): boolean {
const quoted = extractQuote(text, range)
const quoted = extractQuote(text, range, false)
if (!quoted) return false

const {container, selectionText} = quoted
Expand Down Expand Up @@ -125,7 +128,7 @@ type Quote = {
selectionText: string
}

function extractQuote(text: string, range: Range): ?Quote {
function extractQuote(text: string, range: Range, unwrap: boolean): ?Quote {
let selectionText = text.trim()
if (!selectionText) return

Expand All @@ -141,7 +144,7 @@ function extractQuote(text: string, range: Range): ?Quote {
const markdownSelector = container.getAttribute('data-quote-markdown')
if (markdownSelector != null) {
try {
selectionText = selectFragment(rangeToMarkdown(range, markdownSelector))
selectionText = selectFragment(rangeToMarkdown(range, markdownSelector, unwrap))
.replace(/^\n+/, '')
.replace(/\s+$/, '')
} catch (error) {
Expand Down

0 comments on commit 9228503

Please sign in to comment.