beancount
    Preparing search index...

    Function parseFile

    • Parses a Beancount file from the filesystem.

      Parameters

      • filepath: string

        Path to the Beancount file to parse

      • options: ParseFileOptions = {}

        Parsing options

      Returns Promise<ParseResult>

      A ParseResult containing all parsed nodes

      Basic usage (Node.js):

      import { parseFile } from 'beancount'

      const result = await parseFile('/path/to/ledger.beancount')

      With recursive parsing of includes (Node.js):

      const result = await parseFile('/path/to/main.beancount', { recurse: true })
      // All nodes from included files are merged into the result

      Browser usage with fetch:

      import { parseFile, type FileSystemHelpers } from 'beancount'

      const browserFS: FileSystemHelpers = {
      readFile: async (path) => {
      const response = await fetch(path)
      return response.text()
      },
      resolvePath: (path) => new URL(path, window.location.origin).pathname,
      resolveRelative: (base, rel) => {
      const baseDir = base.substring(0, base.lastIndexOf('/') + 1)
      return new URL(rel, window.location.origin + baseDir).pathname
      },
      dirname: (path) => path.substring(0, path.lastIndexOf('/')),
      }

      const result = await parseFile('/api/ledger.beancount', {
      recurse: true,
      fs: browserFS,
      })

      Deno usage:

      import { parseFile, type FileSystemHelpers } from 'npm:beancount'

      const denoFS: FileSystemHelpers = {
      readFile: async (path) => await Deno.readTextFile(path),
      resolvePath: (path) => new URL(path, import.meta.url).pathname,
      resolveRelative: (base, rel) => {
      const baseDir = base.substring(0, base.lastIndexOf('/') + 1)
      return baseDir + rel
      },
      dirname: (path) => path.substring(0, path.lastIndexOf('/')),
      }

      const result = await parseFile('./ledger.beancount', {
      recurse: true,
      fs: denoFS,
      })