parse5
    Preparing search index...

    Converts plain text files into HTML document as required by HTML specification. A writable stream.

    const PlainTextConversionStream = require('parse5-plain-text-conversion-stream');
    const fs = require('fs');
    const { finished } = require('node:stream');

    const file = fs.createReadStream('war_and_peace.txt');
    const converter = new PlainTextConversionStream();

    finished(converter, () => {
    console.log(converter.document.childNodes[1].childNodes[0].tagName); //> 'head'
    });

    file.pipe(converter);

    Type Parameters

    Hierarchy (View Summary)

    Index
    parser: Parser<T>
    • get document(): T["document"]

      The resulting document node.

      Returns T["document"]

    • Parameters

      • chunk: string
      • _encoding: string
      • callback: () => void

      Returns void

    • Calling the writable.end() method signals that no more data will be written to the Writable. The optional chunk and encoding arguments allow one final additional chunk of data to be written immediately before closing the stream.

      Calling the write method after calling end will raise an error.

      // Write 'hello, ' and then end with 'world!'.
      import fs from 'node:fs';
      const file = fs.createWriteStream('example.txt');
      file.write('hello, ');
      file.end('world!');
      // Writing more now is not allowed!

      Parameters

      • Optionalchunk: any

        Optional data to write. For streams not operating in object mode, chunk must be a {string}, {Buffer}, {TypedArray} or {DataView}. For object mode streams, chunk may be any JavaScript value other than null.

      • Optionalencoding: any

        The encoding if chunk is a string

      • Optionalcallback: any

        Callback for when the stream is finished.

      Returns any

      v0.9.4

    • Synchronously delivers buffered character tokens to the tree builder without ending the input stream. Incomplete tags and character references are retained for subsequent writes. This does not resume a suspended script or process queued writes, and must not be called from a parser callback.

      Flushing splits character-token runs. Their source locations do not extend through subsequently ignored input, such as CDATA delimiters or the leading newline in a pre element, so text-node source ranges can differ from parsing without flushing.

      Returns void

      const stream = new ParserStream();
      stream.write('<p>hello');
      stream.flushCharacters();
      // The paragraph's text is now available in `stream.document`.
      stream.end(' world</p>');
    • Returns T["documentFragment"]

    • Raised when parser encounters a <script> element. If this event has listeners, parsing will be suspended once it is emitted. So, if <script> has the src attribute, you can fetch it, execute and then resume parsing just like browsers do.

      Parameters

      • event: "script"

        Name of the event

      • handler: (
            scriptElement: T["element"],
            documentWrite: (html: string) => void,
            resume: () => void,
        ) => void

      Returns void

      const ParserStream = require('parse5-parser-stream');
      const http = require('http');

      const parser = new ParserStream();

      parser.on('script', (scriptElement, documentWrite, resume) => {
      const src = scriptElement.attrs.find(({ name }) => name === 'src').value;

      http.get(src, res => {
      // Fetch the script content, execute it with DOM built around `parser.document` and
      // `document.write` implemented using `documentWrite`.
      ...
      // Then resume parsing.
      resume();
      });
      });

      parser.end('<script src="example.com/script.js"></script>');
    • Base event handler.

      Parameters

      • event: string

        Name of the event

      • handler: (...args: any[]) => void

        Event handler

      Returns this

    • Type Parameters

      • T extends TreeAdapterTypeMap<
            unknown,
            unknown,
            unknown,
            unknown,
            unknown,
            unknown,
            unknown,
            unknown,
            unknown,
            unknown,
        >

      Parameters

      • OptionalfragmentContext: T["parentNode"] | null
      • Optionaloptions: ParserOptions<T>

      Returns ParserStream<T>