Skip to content

Commit

Permalink
Use @file-type/xml as a detector example
Browse files Browse the repository at this point in the history
Adds the following to README:
- Refer to `@file-type/xml` for XML based formats
- Use `@file-type/xml` to demonstrate adding a custom detector
  • Loading branch information
Borewit committed Dec 19, 2024
1 parent 7e72bbc commit 98d3879
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 11 additions & 1 deletion core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ A custom file type detector.
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
### Example adding a detector
```js
import {FileTypeParser} from 'file-type';
import {detectXml} from '@file-type/xml'; // Third party detector for XML formats
const parser = new FileTypeParser({customDetectors: [detectXml]});
const fileType = await parser.fromFile('sample.kml');
console.log(fileType);
```
Detectors provided through the constructor options are executed before the default detectors.
Custom detectors allow for:
Expand All @@ -131,7 +141,7 @@ If a detector returns `undefined`, the following rules apply:
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
2. **Tokenizer Interaction**: If the detector modifies the tokenizer's position (`tokenizer.position` is advanced), no further detectors are executed. In this case, the file type remains `undefined`, as subsequent detectors cannot evaluate the content. This is an exceptional scenario, as it prevents any other detectors from determining the file type.
### Example usage
### Example writing a custom detector
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
Expand Down
15 changes: 13 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ A custom file type detector.
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
### Example adding a detector
```js
import {FileTypeParser} from 'file-type';
import {detectXml} from '@file-type/xml'; // Third party detector for XML formats

const parser = new FileTypeParser({customDetectors: [detectXml]});
const fileType = await parser.fromFile('sample.kml');
console.log(fileType);
```
Detectors provided through the constructor options are executed before the default detectors.
Custom detectors allow for:
Expand All @@ -357,7 +368,7 @@ If a detector returns `undefined`, the following rules apply:
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
2. **Tokenizer Interaction**: If the detector modifies the tokenizer's position (`tokenizer.position` is advanced), no further detectors are executed. In this case, the file type remains `undefined`, as subsequent detectors cannot evaluate the content. This is an exceptional scenario, as it prevents any other detectors from determining the file type.
### Example usage
### Example writing a custom detector
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
Expand Down Expand Up @@ -578,7 +589,7 @@ The following file types will not be accepted:
- `.ppt` - Microsoft PowerPoint97-2003 Document
- `.msi` - Microsoft Windows Installer
- `.csv` - [Reason.](https://github.com/sindresorhus/file-type/issues/264#issuecomment-568439196)
- `.svg` - Detecting it requires a full-blown parser. Check out [`is-svg`](https://github.com/sindresorhus/is-svg) for something that mostly works.
- `.svg` - You can add [@file-type/xml](https://github.com/Borewit/file-type-xml) to detect XML based formats.
#### tokenizer
Expand Down

0 comments on commit 98d3879

Please sign in to comment.