zcap-spec-examples - v0.2.0
    Preparing search index...

    Function extractExamples

    • Extract every example from a ReSpec/zcap-spec document, streaming.

      This is the main entry point of the library. It accepts a Response, a ReadableStream, any async iterable of chunks, or a plain string, and yields one ZcapSpecExample at a time, in document order.

      Nothing accumulates: each example is emitted as soon as its closing tag arrives and is then dropped, so memory stays flat regardless of document size. You never have to hold the whole spec as a string.

      Examples are recognised by a class="example" on an <aside>, <div>, <figure>, or <pre>, which covers the shapes different ReSpec versions produce. Markup nested inside the example (syntax-highlighting <span>s and the like) is flattened away and HTML entities are decoded, so content is the example text as an author would write it.

      Parameters

      Returns AsyncGenerator<ZcapSpecExample>

      An async iterable of examples, in document order.

      const response = await fetch("https://w3c-ccg.github.io/zcap-spec/v0.4.0-draft/");

      for await (const example of extractExamples(response)) {
      console.log(example.name, example.url, example.mediaType);
      }
      const examples = await Array.fromAsync(extractExamples(response));
      
      import { createReadStream } from "node:fs";
      for await (const example of extractExamples(createReadStream("index.html"))) {
      // ...
      }