TrialAndErrorOrg/parsers

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

Closed this issue · 1 comments

[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