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] Figure out how to properly nest environments and commands #7

Closed github-actions[bot] closed 3 months ago

github-actions[bot] commented 2 years ago

[jast-util-to-texast] Figure out how to properly nest environments and commands

@ts-ignore

https://github.com/JournalOfTrialAndError/JOTE/blob/9dbab5875d891f5e94f9627c3ae5c3a93b743613/libs/rejour/jast-util-to-texast/src/lib/handlers/sec.ts#L45


import { isElement, Title } from 'jast'
import { CommandArg, CommandArgOpt, EnvironmentContent } from 'texast'
import { all } from '../all'
import { J, Parents, TagName, Node } from '../types'
import { wrap } from '../util/wrap'
import { wrapChildren } from '../util/wrap-children'
import { wrapCommandArg } from '../util/wrap-command-arg'

export const sectionDepth = [
  'part',
  'chapter',
  'section',
  'subsection',
  'subsubsection',
  'paragraph',
  'subparagraph',
  'textbf',
]
export function sec(j: J, node: Parents) {
  let titleElement: Title | null = null

  const sectionArg = sectionDepth[(j.sectionDepth + 1) % sectionDepth.length]

  for (let i = 0; i < node?.children?.length || 0; i++) {
    const child = node?.children[i]
    if (isElement(child) && child.tagName === 'title') {
      node.children.splice(i, 1)
      titleElement = child
      break
    }
  }

  j.sectionDepth++
  const contents = all(j, node)
  j.sectionDepth--
  if (!titleElement) return contents

  contents.unshift({
    type: 'command',
    name: sectionArg,
    children: [
      {
        type: 'commandArg',
        optional: false,
        // TODO: [jast-util-to-texast] Figure out how to properly nest environments and commands
        // @ts-ignore
        children: all(j, titleElement),
      },
    ],
  })

  return wrap(contents)
}

7b133727cabb8d4c68e46fda22bbc09e8e95d51a

github-actions[bot] commented 3 months ago

Closed in 63a6ca2cbd1729dd27de48746d5ff25c09016822