TrialAndErrorOrg / parsers

Monorepo for a suite of `unified`-compatible converters for converting between, from, and to .docx, JATS XML, LaTeX, and PDF
https://convert.centeroftrialanderror.com
GNU General Public License v3.0
15 stars 0 forks source link

[jast-util-to-texast] Store information about column alignment in tabular #9

Closed github-actions[bot] closed 3 months ago

github-actions[bot] commented 2 years ago

[jast-util-to-texast] Store information about column alignment in tabular

https://github.com/JournalOfTrialAndError/JOTE/blob/131a791996b998f95b72e1cc839ab710bb021681/libs/rejour/jast-util-to-texast/src/lib/handlers/table.ts#L16


// based on https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/handlers/em

import { Table, isElement, Element } from 'jjast'
import { all } from '../all'
import { J, Node } from '../types'
import { visit as origVisit } from 'unist-util-visit'
import { CommandArg } from 'texast'

// try to turn of typechecking for visit as it it bugged
// https://github.com/syntax-tree/unist-util-visit/issues/33
const visit = origVisit as any
export function table(j: J, table: Table) {
  let numberOfColumns = 0
  let hasCols = false
  // tables can be def'd in terms of cols or rows
  // TODO: [jast-util-to-texast] Store information about column alignment in tabular
  visit(
    table,
    (node: Node) => isElement(node) && ['col', 'tr'].includes(node.tagName),
    (node: Element) => {
      if (node.tagName === 'col') {
        hasCols = true
        numberOfColumns++
        return
      }

      if (hasCols) return
      visit(node, 'td', (td: Node) => {
        numberOfColumns++
      })
      hasCols = true
    }
  )

  const colAlignment = new Array(numberOfColumns).map((col) => 'c').join(' | ')
  const colAlignArg: CommandArg = {
    type: 'commandArg',
    children: [{ type: 'text', value: colAlignment }],
  }

  const contents = all(j, table)
  contents.unshift(colAlignArg)

  return { type: 'environment', name: 'tabular', children: contents }
}

aada405ae57440e1136dbc840d7c870c25dc9c89

github-actions[bot] commented 3 months ago

Closed in 63a6ca2cbd1729dd27de48746d5ff25c09016822

github-actions[bot] commented 3 months ago

Closed in a56d7d8a39b78ce5dcc0a53143cf5dd993440013