From 164bb66294081740429b732923a861bcbbd50430 Mon Sep 17 00:00:00 2001 From: Krystan HuffMenne Date: Tue, 19 Dec 2023 09:22:00 -0800 Subject: [PATCH] Document API methods (Closes #45) --- README.md | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/README.md b/README.md index 3fa81b4..9d259e3 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,97 @@ let output = p.process(''); console.log(output); ``` +## API + +### `Preprocessor` + +All `content-tag` public API lives on the `Preprocessor` object. + +### `Preprocessor.process(src: string, filename?: string): string;` + +Parses a given source code string using the `content-tag` spec into standard +JavaScript. + +```ts +import { Preprocessor } from 'content-tag'; +let p = new Preprocessor(); +let output = p.process(''); +``` + +### `Preprocessor.parse(src: string, filename?: string): Parsed[];` + +Parses a given source code string using the `content-tag` spec into an array of +`Parsed` content tag objects. + +```ts +import { Preprocessor } from 'content-tag'; +let p = new Preprocessor(); +let output = p.parse(''); +``` + +#### `Parsed` + +NOTE: All ranges are in bytes, not characters. + +````ts +interface Parsed { + /** + * The type for the content tag. + * + * 'expression' corresponds to a tag in an expression position, e.g. + * ``` + * const HiComponent = ; + * ``` + * + * 'class-member' corresponds to a tag in a class-member position, e.g. + * ``` + * export default class HiComponent extends Component { + * + * } + * ``` + */ + type: "expression" | "class-member"; + + /** + * Currently, only template tags are parsed. + */ + tagName: "template"; + + /** Raw template contents. */ + contents: string; + + /** + * Byte range of the contents, inclusive of inclusive of the + * `` tags. + */ + range: { + start: number; + end: number; + }; + + /** + * Byte range of the template contents, not inclusive of the + * `` tags. + */ + contentRange: { + start: number; + end: number; + }; + + /** Byte range of the opening `` tag. */ + endRange: { + start: number; + end: number; + }; +} +```` + ## Contributing See the [CONTRIBUTING.md](./CONTRIBUTING.md) file.