Skip to content

Functions:documentParser

Emiel Wit edited this page Jan 18, 2024 · 8 revisions

Document Parser

Description

The documentParser function is an asynchronous function that takes in DocumentParserArguments as a parameter and returns a Promise that resolves to DocumentParserResult. It is used to parse a document and extract the text content from it.

Parameters

interface DocumentParserArguments {
  document: string;
  parserOptions?: {
    forceImage?: boolean;
    density?: number;
  };
}

interface DocumentParserResult {
  result: string;
}

Returns

A Promise that resolves to DocumentParserResult which is an object that contains the result which is the extracted text from the parsed document.

Example

const isFileProperty = (value) =>
  value && typeof value === 'object' && 'url' in value;

const parseDocument = async ({ document, density, forceImage }) => {
  const url = isFileProperty(document) ? document?.url : document;

  const { result } = await documentParser({
    document: url,
    parseOptions: { density, forceImage },
  });

  return {
    result,
  };
};

export default parseDocument;

Clone this wiki locally