Skip to content

Commit

Permalink
Merge pull request #57 from gitKrystan/api-docs
Browse files Browse the repository at this point in the history
Document API methods (Closes #45)
  • Loading branch information
ef4 authored Dec 19, 2023
2 parents 29d282e + 164bb66 commit 384c86b
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,97 @@ let output = p.process('<template>Hi</template>');
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('<template>Hi</template>');
```

### `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('<template>Hi</template>');
```

#### `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 = <template>Hi</template>;
* ```
*
* 'class-member' corresponds to a tag in a class-member position, e.g.
* ```
* export default class HiComponent extends Component {
* <template>Hi</template>
* }
* ```
*/
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
* `<template></template>` tags.
*/
range: {
start: number;
end: number;
};

/**
* Byte range of the template contents, not inclusive of the
* `<template></template>` tags.
*/
contentRange: {
start: number;
end: number;
};

/** Byte range of the opening `<template>` tag. */
startRange: {
end: number;
start: number;
};

/** Byte range of the closing `</template>` tag. */
endRange: {
start: number;
end: number;
};
}
````

## Contributing

See the [CONTRIBUTING.md](./CONTRIBUTING.md) file.
Expand Down

0 comments on commit 384c86b

Please sign in to comment.