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

[rejour-relatex] Cast JastRoot to TexastRoot better #14

Closed github-actions[bot] closed 3 months ago

github-actions[bot] commented 2 years ago

[rejour-relatex] Cast JastRoot to TexastRoot better

@ts-ignore there should be a better way to cast this

https://github.com/JournalOfTrialAndError/JOTE/blob/b3f55b145db6127b88ab8c195e3ca988dc4ee5bb/libs/reoff/reoff-rejour/src/lib/reoff-reoff-rejour.ts#L39


import { toTexast, Options } from 'jast-util-to-texast'
import { Root as JastRoot } from 'jjast'
import { Root as TexastRoot } from 'texast'
import {
  Plugin,
  Processor as UnifiedProcessor,
  TransformCallback,
  Transformer,
} from 'unified'
import { VFile } from 'vfile'
type Processor = UnifiedProcessor<any, any, any, any>

/**
 * Bridge-mode.
 * Runs the destination with the new mdast tree.
 *
 */
function bridge(
  destination: Processor,
  options?: Options
): void | Transformer<JastRoot, JastRoot> {
  return (node, file, next) => {
    //@ts-ignore there should be a better way to cast this
    destination.run(toTexast(node, options), file, (error) => {
      next(error)
    })
  }
}

/**
 * Mutate-mode.
 * Further transformers run on the texast tree.
 */
function mutate(
  options: void | Options | undefined = {}
): ReturnType<Plugin<[Options?] | void[], JastRoot, TexastRoot>> {
  //Transformer<JastRoot, JastRoot> | void {
  return (node) => {
    // TODO: [rejour-relatex] Cast JastRoot to TexastRoot better
    //@ts-ignore there should be a better way to cast this
    const result = toTexast(node, options) as TexastRoot
    return result
  }
}

/**
 * Plugin to bridge or mutate to rehype.
 *
 * If a destination is given, runs the destination with the new mdast
 * tree (bridge-mode).
 * Without destination, returns the mdast tree: further plugins run on that
 * tree (mutate-mode).
 *
 * @param destination
 *   Optional unified processor.
 * @param options
 *   Options passed to `jast-util-to-texast`.
 */
const rejourRelatex = function (
  destination?: Processor | Options,
  options?: Options
) {
  let settings: Options | undefined
  let processor: Processor | undefined

  if (typeof destination === 'function') {
    processor = destination
    settings = options
  } else {
    settings = destination
  }

  if (settings?.document === undefined || settings.document === null) {
    settings = Object.assign({}, settings, { document: true })
  }

  return processor ? bridge(processor, settings) : mutate(settings)
} as Plugin<[Processor, Options?], JastRoot> &
  Plugin<[Options?] | void[], JastRoot, TexastRoot>

export default rejourRelatex

3aadda35ede377c7bb173f0a2685a3c928807fc7

github-actions[bot] commented 3 months ago

Closed in 63a6ca2cbd1729dd27de48746d5ff25c09016822

github-actions[bot] commented 3 months ago

Closed in a56d7d8a39b78ce5dcc0a53143cf5dd993440013

github-actions[bot] commented 3 months ago

Closed in e7fb7dd808d078468892d31b0132faf4508a172b