Skip to content

Commit

Permalink
Document API methods (Closes embroider-build#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Dec 19, 2023
1 parent 29d282e commit 1659b17
Showing 1 changed file with 98 additions and 9 deletions.
107 changes: 98 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,125 @@ npm install content-tag

### Node (CommonJS)

```js
let { Preprocessor } = require('content-tag');
```js
let { Preprocessor } = require("content-tag");
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');
let output = p.process("<template>Hi</template>");

console.log(output);
```


### Node (ESM)

```js
import { Preprocessor } from 'content-tag';
import { Preprocessor } from "content-tag";
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');
let output = p.process("<template>Hi</template>");

console.log(output);
```

### Browser (ESM)

```js
import { Preprocessor } from 'content-tag';
import { Preprocessor } from "content-tag";
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');
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.

0 comments on commit 1659b17

Please sign in to comment.