| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920 |
- import { EditorView } from '@codemirror/view'
- import { Prec } from '@codemirror/state'
- import {
- insertPastedContent,
- pastedContent,
- storePastedContent,
- } from './pasted-content'
- import { debugConsole } from '@/utils/debugging'
- import {
- isBlockContainingElement,
- isBlockElement,
- isElementNode,
- isInlineElement,
- isTextNode,
- shouldRemoveEmptyBlockElement,
- } from './html-elements'
- export const pasteHtml = [
- Prec.highest(
- EditorView.domEventHandlers({
- paste(event, view) {
- const { clipboardData } = event
- if (!clipboardData) {
- return false
- }
- // only handle pasted HTML
- if (!clipboardData.types.includes('text/html')) {
- return false
- }
- // ignore text/html from VS Code
- if (
- clipboardData.types.includes('application/vnd.code.copymetadata') ||
- clipboardData.types.includes('vscode-editor-data')
- ) {
- return false
- }
- const html = clipboardData.getData('text/html').trim()
- const text = clipboardData.getData('text/plain').trim()
- if (html.length === 0) {
- return false
- }
- // convert the HTML to LaTeX
- try {
- const parser = new DOMParser()
- const { documentElement } = parser.parseFromString(html, 'text/html')
- // fall back to creating a figure when there's an image on the clipoard,
- // unless the HTML indicates that it came from an Office application
- // (which also puts an image on the clipboard)
- if (
- clipboardData.files.length > 0 &&
- !hasProgId(documentElement) &&
- !isOnlyTable(documentElement)
- ) {
- return false
- }
- const bodyElement = documentElement.querySelector('body')
- // DOMParser should always create a body element, so this is mostly for TypeScript
- if (!bodyElement) {
- return false
- }
- // if the only content is in a code block, use the plain text version
- if (onlyCode(bodyElement)) {
- return false
- }
- const latex = htmlToLaTeX(bodyElement)
- // if there's no formatting, use the plain text version
- if (latex === text && clipboardData.files.length === 0) {
- return false
- }
- view.dispatch(insertPastedContent(view, { latex, text }))
- view.dispatch(storePastedContent({ latex, text }, true))
- return true
- } catch (error) {
- debugConsole.error(error)
- // fall back to the default paste handler
- return false
- }
- },
- })
- ),
- pastedContent,
- ]
- const removeUnwantedElements = (
- documentElement: HTMLElement,
- selector: string
- ) => {
- for (const element of documentElement.querySelectorAll(selector)) {
- element.remove()
- }
- }
- const findCodeContainingElement = (documentElement: HTMLElement) => {
- let result: HTMLElement | null
- // a code element
- result = documentElement.querySelector<HTMLElement>('code')
- if (result) {
- return result
- }
- // a pre element with "monospace" somewhere in the font family
- result = documentElement.querySelector<HTMLPreElement>('pre')
- if (result?.style.fontFamily.includes('monospace')) {
- return result
- }
- return null
- }
- // return true if the text content of the first <code> element
- // is the same as the text content of the whole document element
- const onlyCode = (documentElement: HTMLElement) => {
- const codeElement = findCodeContainingElement(documentElement)
- return (
- codeElement?.textContent?.trim() === documentElement.textContent?.trim()
- )
- }
- const hasProgId = (documentElement: HTMLElement) => {
- const meta = documentElement.querySelector<HTMLMetaElement>(
- 'meta[name="ProgId"]'
- )
- return meta && meta.content.trim().length > 0
- }
- // detect a table (probably pasted from desktop Excel)
- const isOnlyTable = (documentElement: HTMLElement) => {
- const body = documentElement.querySelector<HTMLBodyElement>('body')
- return (
- body &&
- body.childElementCount === 1 &&
- body.firstElementChild!.nodeName === 'TABLE'
- )
- }
- const htmlToLaTeX = (bodyElement: HTMLElement) => {
- // remove style elements
- removeUnwantedElements(bodyElement, 'style')
- let before: string | null = null
- let after: string | null = null
- // repeat until the content stabilises
- do {
- before = bodyElement.textContent
- // normalise whitespace in text
- normaliseWhitespace(bodyElement)
- // replace unwanted whitespace in blocks
- processWhitespaceInBlocks(bodyElement)
- after = bodyElement.textContent
- } while (before !== after)
- // pre-process table elements
- processTables(bodyElement)
- // pre-process lists
- processLists(bodyElement)
- // protect special characters in non-LaTeX text nodes
- protectSpecialCharacters(bodyElement)
- processMatchedElements(bodyElement)
- const text = bodyElement.textContent
- if (!text) {
- return ''
- }
- return (
- text
- // remove zero-width spaces (e.g. those added by Powerpoint)
- .replaceAll('', '')
- // normalise multiple newlines
- .replaceAll(/\n{2,}/g, '\n\n')
- // only allow a single newline at the start and end
- .replaceAll(/(^\n+|\n+$)/g, '\n')
- // replace tab with 4 spaces (hard-coded indent unit)
- .replaceAll('\t', ' ')
- )
- }
- const trimInlineElements = (
- element: HTMLElement,
- precedingSpace = true
- ): boolean => {
- for (const node of element.childNodes) {
- if (isTextNode(node)) {
- let text = node.textContent!
- if (precedingSpace) {
- text = text.replace(/^\s+/, '')
- }
- if (text === '') {
- node.remove()
- } else {
- node.textContent = text
- precedingSpace = /\s$/.test(text)
- }
- } else if (isInlineElement(node)) {
- precedingSpace = trimInlineElements(node, precedingSpace)
- } else if (isBlockElement(node)) {
- precedingSpace = true // TODO
- } else {
- precedingSpace = false // TODO
- }
- }
- // TODO: trim whitespace at the end
- return precedingSpace
- }
- const processWhitespaceInBlocks = (documentElement: HTMLElement) => {
- trimInlineElements(documentElement)
- const walker = document.createTreeWalker(
- documentElement,
- NodeFilter.SHOW_ELEMENT,
- node =>
- isElementNode(node) && isElementContainingCode(node)
- ? NodeFilter.FILTER_REJECT
- : NodeFilter.FILTER_ACCEPT
- )
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
- // TODO: remove leading newline from pre, code and textarea?
- if (isBlockContainingElement(node)) {
- // remove all text nodes directly inside elements that should only contain blocks
- for (const childNode of node.childNodes) {
- if (isTextNode(childNode)) {
- childNode.remove()
- }
- }
- }
- if (isBlockElement(node)) {
- trimInlineElements(node)
- if (shouldRemoveEmptyBlockElement(node)) {
- node.remove()
- // TODO: and parents?
- }
- }
- }
- }
- const normaliseWhitespace = (documentElement: HTMLElement) => {
- const walker = document.createTreeWalker(
- documentElement,
- NodeFilter.SHOW_TEXT,
- node =>
- isElementNode(node) && isElementContainingCode(node)
- ? NodeFilter.FILTER_REJECT
- : NodeFilter.FILTER_ACCEPT
- )
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
- const text = node.textContent
- if (text !== null) {
- if (/^\s+$/.test(text)) {
- // replace nodes containing only whitespace (including non-breaking space) with a single space
- node.textContent = ' '
- } else {
- // collapse contiguous whitespace (except for non-breaking space) to a single space
- node.textContent = text.replaceAll(/[\n\r\f\t \u2028\u2029]+/g, ' ')
- }
- }
- }
- }
- // TODO: negative lookbehind once Safari supports it
- const specialCharacterRegExp = /(^|[^\\])([#$%&~_^\\{}])/g
- const specialCharacterReplacer = (
- _match: string,
- prefix: string,
- char: string
- ) => {
- if (char === '\\') {
- // convert `\` to `\textbackslash{}`, preserving subsequent whitespace
- char = 'textbackslash{}'
- }
- return `${prefix}\\${char}`
- }
- const isElementContainingCode = (element: HTMLElement) =>
- element.nodeName === 'CODE' ||
- (element.nodeName === 'PRE' && element.style.fontFamily.includes('monospace'))
- const protectSpecialCharacters = (documentElement: HTMLElement) => {
- const walker = document.createTreeWalker(
- documentElement,
- NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
- node =>
- isElementNode(node) && isElementContainingCode(node)
- ? NodeFilter.FILTER_REJECT
- : NodeFilter.FILTER_ACCEPT
- )
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
- if (isTextNode(node)) {
- const text = node.textContent
- if (text) {
- // replace non-backslash-prefixed characters
- node.textContent = text.replaceAll(
- specialCharacterRegExp,
- specialCharacterReplacer
- )
- }
- }
- }
- }
- const processMatchedElements = (documentElement: HTMLElement) => {
- for (const item of selectors) {
- for (const element of documentElement.querySelectorAll<any>(
- item.selector
- )) {
- if (!item.match || item.match(element)) {
- // start the markup
- if (item.start) {
- const start = document.createTextNode(item.start(element))
- if (item.inside) {
- element.prepend(start)
- } else {
- element.before(start)
- }
- }
- // end the markup
- if (item.end) {
- const end = document.createTextNode(item.end(element))
- if (item.inside) {
- element.append(end)
- } else {
- element.after(end)
- }
- }
- }
- }
- }
- }
- const matchingParents = (element: HTMLElement, selector: string) => {
- const matches = []
- for (
- let ancestor = element.parentElement?.closest(selector);
- ancestor;
- ancestor = ancestor.parentElement?.closest(selector)
- ) {
- matches.push(ancestor)
- }
- return matches
- }
- const urlCharacterReplacements = new Map<string, string>([
- ['\\', '\\\\'],
- ['#', '\\#'],
- ['%', '\\%'],
- ['{', '%7B'],
- ['}', '%7D'],
- ])
- const protectUrlCharacters = (url: string) => {
- // NOTE: add new characters to both this regex and urlCharacterReplacements
- return url.replaceAll(/[\\#%{}]/g, match => {
- const replacement = urlCharacterReplacements.get(match)
- if (!replacement) {
- throw new Error(`No replacement found for ${match}`)
- }
- return replacement
- })
- }
- const processLists = (element: HTMLElement) => {
- for (const list of element.querySelectorAll('ol,ul')) {
- // if the list has only one item, replace the list with an element containing the contents of the item
- if (list.childElementCount === 1) {
- const div = document.createElement('div')
- div.append(...list.firstElementChild!.childNodes)
- list.before('\n', div, '\n')
- list.remove()
- }
- }
- }
- const processTables = (element: HTMLElement) => {
- for (const table of element.querySelectorAll('table')) {
- // create a wrapper element for the table and the caption
- const container = document.createElement('div')
- container.className = 'ol-table-wrap'
- table.after(container)
- // move the caption (if it exists) into the container before the table
- const caption = table.querySelector('caption')
- if (caption) {
- container.append(caption)
- }
- // move the table into the container
- container.append(table)
- // add empty cells to account for rowspan
- for (const cell of table.querySelectorAll<HTMLTableCellElement>(
- 'th[rowspan],td[rowspan]'
- )) {
- const rowspan = Number(cell.getAttribute('rowspan') || '1')
- const colspan = Number(cell.getAttribute('colspan') || '1')
- let row: HTMLTableRowElement | null = cell.closest('tr')
- if (row) {
- let position = 0
- for (const child of row.cells) {
- if (child === cell) {
- break
- }
- position += Number(child.getAttribute('colspan') || '1')
- }
- for (let i = 1; i < rowspan; i++) {
- const nextElement: Element | null = row?.nextElementSibling
- if (!isTableRow(nextElement)) {
- break
- }
- row = nextElement
- let targetCell: HTMLTableCellElement | undefined
- let targetPosition = 0
- for (const child of row.cells) {
- if (targetPosition === position) {
- targetCell = child
- break
- }
- targetPosition += Number(child.getAttribute('colspan') || '1')
- }
- const fillerCells = Array.from({ length: colspan }, () =>
- document.createElement('td')
- )
- if (targetCell) {
- targetCell.before(...fillerCells)
- } else {
- row.append(...fillerCells)
- }
- }
- }
- }
- }
- }
- const isTableRow = (element: Element | null): element is HTMLTableRowElement =>
- element?.nodeName === 'TR'
- const cellAlignment = new Map([
- ['left', 'l'],
- ['center', 'c'],
- ['right', 'r'],
- ])
- const tabular = (element: HTMLTableElement) => {
- const definitions: Array<{
- alignment: string
- borderLeft: boolean
- borderRight: boolean
- }> = []
- const rows = element.querySelectorAll('tr')
- for (const row of rows) {
- const cells = [...row.childNodes].filter(
- element => element.nodeName === 'TD' || element.nodeName === 'TH'
- ) as Array<HTMLTableCellElement>
- let index = 0
- for (const cell of cells) {
- // NOTE: reading the alignment and borders from the first cell definition in each column
- if (definitions[index] === undefined) {
- const { textAlign, borderLeftStyle, borderRightStyle } = cell.style
- definitions[index] = {
- alignment: textAlign,
- borderLeft: visibleBorderStyle(borderLeftStyle),
- borderRight: visibleBorderStyle(borderRightStyle),
- }
- }
- index += Number(cell.getAttribute('colspan') ?? 1)
- }
- }
- for (let index = 0; index <= definitions.length; index++) {
- // fill in missing definitions
- const item = definitions[index] || {
- alignment: 'left',
- borderLeft: false,
- borderRight: false,
- }
- // remove left border if previous column had a right border
- if (item.borderLeft && index > 0 && definitions[index - 1]?.borderRight) {
- item.borderLeft = false
- }
- }
- return definitions
- .flatMap(definition => [
- definition.borderLeft ? '|' : '',
- cellAlignment.get(definition.alignment) ?? 'l',
- definition.borderRight ? '|' : '',
- ])
- .filter(Boolean)
- .join(' ')
- }
- const listDepth = (element: HTMLElement): number =>
- Math.max(0, matchingParents(element, 'ul,ol').length)
- const indentUnit = ' ' // TODO: replace hard-coded indent unit?
- const listIndent = (element: HTMLElement | null): string =>
- element ? indentUnit.repeat(listDepth(element)) : ''
- type ElementSelector<T extends string, E extends HTMLElement = HTMLElement> = {
- selector: T
- match?: (element: E) => boolean
- start?: (element: E) => string
- end?: (element: E) => string
- inside?: boolean
- }
- const createSelector = <
- T extends string,
- E extends HTMLElement = T extends keyof HTMLElementTagNameMap
- ? HTMLElementTagNameMap[T]
- : HTMLElement,
- >({
- selector,
- ...elementSelector
- }: ElementSelector<T, E>) => ({
- selector,
- ...elementSelector,
- })
- const headings = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']
- const isHeading = (element: HTMLElement | null) => {
- return element && headings.includes(element.nodeName)
- }
- const hasContent = (element: HTMLElement): boolean => {
- return Boolean(element.textContent && element.textContent.trim().length > 0)
- }
- type BorderStyle =
- | 'borderTopStyle'
- | 'borderRightStyle'
- | 'borderBottomStyle'
- | 'borderLeftStyle'
- const visibleBorderStyle = (style: CSSStyleDeclaration[BorderStyle]): boolean =>
- !!style && style !== 'none' && style !== 'hidden'
- const rowHasBorderStyle = (
- element: HTMLTableRowElement,
- style: BorderStyle
- ): boolean => {
- if (visibleBorderStyle(element.style[style])) {
- return true
- }
- const cells = element.querySelectorAll<HTMLTableCellElement>('th,td')
- return [...cells].every(cell => visibleBorderStyle(cell.style[style]))
- }
- const isTableRowElement = (
- element: Element | null
- ): element is HTMLTableRowElement => element?.nodeName === 'TR'
- const nextRowHasBorderStyle = (
- element: HTMLTableRowElement,
- style: BorderStyle
- ) => {
- const { nextElementSibling } = element
- return (
- isTableRowElement(nextElementSibling) &&
- rowHasBorderStyle(nextElementSibling, style)
- )
- }
- const startMulticolumn = (element: HTMLTableCellElement): string => {
- const colspan = Number(element.getAttribute('colspan') || 1)
- const alignment = cellAlignment.get(element.style.textAlign) ?? 'l'
- return `\\multicolumn{${colspan}}{${alignment}}{`
- }
- const startMultirow = (element: HTMLTableCellElement): string => {
- const rowspan = Number(element.getAttribute('rowspan') || 1)
- // NOTE: it would be useful to read cell width if specified, using `*` as a starting point
- return `\\multirow{${rowspan}}{*}{`
- }
- const listPrefix = (element: HTMLOListElement | HTMLUListElement) => {
- if (isListOrListItemElement(element.parentElement)) {
- // within a list = newline
- return '\n'
- }
- // outside a list = double newline
- return '\n\n'
- }
- const listSuffix = (element: HTMLOListElement | HTMLUListElement) => {
- if (listDepth(element) === 0) {
- // a top-level list => newline
- return '\n'
- } else {
- // a nested list => no extra newline
- return ''
- }
- }
- const isListElement = (
- element: Element | null
- ): element is HTMLOListElement | HTMLUListElement =>
- element !== null && listNodeNames.includes(element.nodeName)
- const isListOrListItemElement = (
- element: Element | null
- ): element is HTMLOListElement | HTMLUListElement =>
- element !== null && (isListElement(element) || element.nodeName === 'LI')
- const listNodeNames = ['OL', 'UL']
- const selectors = [
- createSelector({
- selector: 'b',
- match: element =>
- !element.style.fontWeight &&
- !isHeading(element.parentElement) &&
- hasContent(element),
- start: () => '\\textbf{',
- end: () => '}',
- }),
- createSelector({
- selector: '*',
- match: element =>
- (element.style.fontWeight === 'bold' ||
- parseInt(element.style.fontWeight) >= 700) &&
- hasContent(element),
- start: () => '\\textbf{',
- end: () => '}',
- inside: true,
- }),
- createSelector({
- selector: 'strong',
- match: element => !element.style.fontWeight && hasContent(element),
- start: () => '\\textbf{',
- end: () => '}',
- }),
- createSelector({
- selector: 'i',
- match: element => !element.style.fontStyle && hasContent(element),
- start: () => '\\textit{',
- end: () => '}',
- }),
- createSelector({
- selector: '*',
- match: element =>
- element.style.fontStyle === 'italic' && hasContent(element),
- start: () => '\\textit{',
- end: () => '}',
- inside: true,
- }),
- createSelector({
- selector: 'em',
- match: element => !element.style.fontStyle && hasContent(element),
- start: () => '\\textit{',
- end: () => '}',
- }),
- createSelector({
- selector: 'sup',
- match: element => !element.style.verticalAlign && hasContent(element),
- start: () => '\\textsuperscript{',
- end: () => '}',
- }),
- createSelector({
- selector: 'span',
- match: element =>
- element.style.verticalAlign === 'super' && hasContent(element),
- start: () => '\\textsuperscript{',
- end: () => '}',
- }),
- createSelector({
- selector: 'sub',
- match: element => !element.style.verticalAlign && hasContent(element),
- start: () => '\\textsubscript{',
- end: () => '}',
- }),
- createSelector({
- selector: 'span',
- match: element =>
- element.style.verticalAlign === 'sub' && hasContent(element),
- start: () => '\\textsubscript{',
- end: () => '}',
- }),
- createSelector({
- selector: 'a',
- match: element => !!element.href && hasContent(element),
- start: (element: HTMLAnchorElement) => {
- const url = protectUrlCharacters(element.href)
- return `\\href{${url}}{`
- },
- end: () => `}`,
- }),
- createSelector({
- selector: 'h1',
- match: element => !element.closest('table') && hasContent(element),
- start: () => `\n\n\\section{`,
- end: () => `}\n\n`,
- }),
- createSelector({
- selector: 'h2',
- match: element => !element.closest('table') && hasContent(element),
- start: () => `\n\n\\subsection{`,
- end: () => `}\n\n`,
- }),
- createSelector({
- selector: 'h3',
- match: element => !element.closest('table') && hasContent(element),
- start: () => `\n\n\\subsubsection{`,
- end: () => `}\n\n`,
- }),
- createSelector({
- selector: 'h4',
- match: element => !element.closest('table') && hasContent(element),
- start: () => `\n\n\\paragraph{`,
- end: () => `}\n\n`,
- }),
- createSelector({
- selector: 'h5',
- match: element => !element.closest('table') && hasContent(element),
- start: () => `\n\n\\subparagraph{`,
- end: () => `}\n\n`,
- }),
- // TODO: h6?
- createSelector({
- selector: 'br',
- match: element => !element.closest('table'),
- start: () => `\n\n`,
- }),
- createSelector({
- selector: 'code',
- match: element =>
- element.parentElement?.nodeName !== 'PRE' && hasContent(element),
- start: () => `\\verb|`,
- end: () => `|`,
- }),
- createSelector({
- selector: 'pre > code',
- match: element => hasContent(element),
- start: () => `\n\n\\begin{verbatim}\n`,
- end: () => `\n\\end{verbatim}\n\n`,
- }),
- createSelector({
- selector: 'pre',
- match: element =>
- element.style.fontFamily.includes('monospace') &&
- element.firstElementChild?.nodeName !== 'CODE' &&
- hasContent(element),
- start: () => `\n\n\\begin{verbatim}\n`,
- end: () => `\n\\end{verbatim}\n\n`,
- }),
- createSelector({
- selector: '.ol-table-wrap',
- start: () => `\n\n\\begin{table}\n\\centering\n`,
- end: () => `\n\\end{table}\n\n`,
- }),
- createSelector({
- selector: 'table',
- start: element => `\n\\begin{tabular}{${tabular(element)}}`,
- end: () => `\\end{tabular}\n`,
- }),
- createSelector({
- selector: 'thead',
- start: () => `\n`,
- end: () => `\n`,
- }),
- createSelector({
- selector: 'tfoot',
- start: () => `\n`,
- end: () => `\n`,
- }),
- createSelector({
- selector: 'tbody',
- start: () => `\n`,
- end: () => `\n`,
- }),
- createSelector({
- selector: 'tr',
- start: element => {
- const borderTop = rowHasBorderStyle(element, 'borderTopStyle')
- return borderTop ? '\\hline\n' : ''
- },
- end: element => {
- const borderBottom = rowHasBorderStyle(element, 'borderBottomStyle')
- return borderBottom && !nextRowHasBorderStyle(element, 'borderTopStyle')
- ? '\n\\hline\n'
- : '\n'
- },
- }),
- createSelector({
- selector: 'tr > td, tr > th',
- start: (element: HTMLTableCellElement) => {
- let output = ''
- const colspan = element.getAttribute('colspan')
- if (colspan && Number(colspan) > 1) {
- output += startMulticolumn(element)
- }
- // NOTE: multirow is nested inside multicolumn
- const rowspan = element.getAttribute('rowspan')
- if (rowspan && Number(rowspan) > 1) {
- output += startMultirow(element)
- }
- return output
- },
- end: element => {
- let output = ''
- // NOTE: multirow is nested inside multicolumn
- const rowspan = element.getAttribute('rowspan')
- if (rowspan && Number(rowspan) > 1) {
- output += '}'
- }
- const colspan = element.getAttribute('colspan')
- if (colspan && Number(colspan) > 1) {
- output += '}'
- }
- const row = element.parentElement as HTMLTableRowElement
- const isLastChild = row.cells.item(row.cells.length - 1) === element
- return output + (isLastChild ? ' \\\\' : ' & ')
- },
- }),
- createSelector({
- selector: 'caption',
- start: () => `\n\n\\caption{`,
- end: () => `}\n\n`,
- }),
- createSelector({
- // selector: 'ul:has(> li:nth-child(2))', // only select lists with at least 2 items (once Firefox supports :has())
- selector: 'ul',
- start: element => {
- return `${listPrefix(element)}${listIndent(element)}\\begin{itemize}`
- },
- end: element => {
- return `\n${listIndent(element)}\\end{itemize}${listSuffix(element)}`
- },
- }),
- createSelector({
- // selector: 'ol:has(> li:nth-child(2))', // only select lists with at least 2 items (once Firefox supports :has())
- selector: 'ol',
- start: element => {
- return `${listPrefix(element)}${listIndent(element)}\\begin{enumerate}`
- },
- end: element => {
- return `\n${listIndent(element)}\\end{enumerate}${listSuffix(element)}`
- },
- }),
- createSelector({
- selector: 'li',
- start: element => {
- return `\n${listIndent(element.parentElement)}${indentUnit}\\item `
- },
- }),
- createSelector({
- selector: 'p',
- match: element => {
- // must have content
- if (!hasContent(element)) {
- return false
- }
- // inside lists and tables, must precede another paragraph
- if (element.closest('li') || element.closest('table')) {
- return element.nextElementSibling?.nodeName === 'P'
- }
- return true
- },
- end: () => '\n\n',
- }),
- createSelector({
- selector: 'blockquote',
- start: () => `\n\n\\begin{quote}\n`,
- end: () => `\n\\end{quote}\n\n`,
- }),
- ]
|