Skip to content

Commit d8d9cbc

Browse files
Document third-party file-type detectors (#708)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 8dfed93 commit d8d9cbc

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

core.d.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,28 @@ export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | u
116116
/**
117117
A custom file type detector.
118118
119+
Custom file type detectors are plugins designed to extend the default detection capabilities.
120+
They allow support for uncommon file types, non-binary formats, or customized detection behavior.
121+
122+
Detectors can be added via the constructor options or by modifying `FileTypeParser#detectors` directly.
123+
Detectors provided through the constructor are executed before the default ones.
124+
119125
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
120126
121-
Detectors provided through the constructor options are executed before the default detectors.
127+
### Example adding a detector
128+
129+
```js
130+
import {FileTypeParser} from 'file-type';
131+
import {detectXml} from '@file-type/xml';
132+
133+
const parser = new FileTypeParser({customDetectors: [detectXml]});
134+
const fileType = await parser.fromFile('sample.kml');
135+
console.log(fileType);
136+
```
137+
138+
### Available-third party file-type detectors
122139
123-
Custom detectors allow for:
124-
- Introducing new `FileTypeResult` entries.
125-
- Modifying the detection behavior of existing `FileTypeResult` types.
140+
- [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as GLM, KML, MusicXML, RSS, SVG, and XHTML
126141
127142
### Detector execution flow
128143
@@ -131,7 +146,7 @@ If a detector returns `undefined`, the following rules apply:
131146
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
132147
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.
133148
134-
### Example usage
149+
### Example writing a custom detector
135150
136151
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
137152

readme.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,28 @@ Returns a `Set<string>` of supported MIME types.
343343
344344
## Custom detectors
345345
346-
A custom file type detector.
346+
Custom file type detectors are plugins designed to extend the default detection capabilities.
347+
They allow support for uncommon file types, non-binary formats, or customized detection behavior.
348+
349+
Detectors can be added via the constructor options or by modifying `FileTypeParser#detectors` directly.
350+
Detectors provided through the constructor are executed before the default ones.
347351
348352
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
349353
350-
Detectors provided through the constructor options are executed before the default detectors.
354+
### Example adding a detector
355+
356+
```js
357+
import {FileTypeParser} from 'file-type';
358+
import {detectXml} from '@file-type/xml';
359+
360+
const parser = new FileTypeParser({customDetectors: [detectXml]});
361+
const fileType = await parser.fromFile('sample.kml');
362+
console.log(fileType);
363+
```
364+
365+
### Available third-party file-type detectors
351366
352-
Custom detectors allow for:
353-
- Introducing new `FileTypeResult` entries.
354-
- Modifying the detection behavior of existing `FileTypeResult` types.
367+
- [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as GLM, KML, MusicXML, RSS, SVG, and XHTML
355368
356369
### Detector execution flow
357370
@@ -360,7 +373,7 @@ If a detector returns `undefined`, the following rules apply:
360373
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
361374
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.
362375
363-
### Example usage
376+
### Writing your own custom detector
364377
365378
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
366379
@@ -597,7 +610,7 @@ The following file types will not be accepted:
597610
- `.ppt` - Microsoft PowerPoint97-2003 Document
598611
- `.msi` - Microsoft Windows Installer
599612
- `.csv` - [Reason.](https://github.com/sindresorhus/file-type/issues/264#issuecomment-568439196)
600-
- `.svg` - Detecting it requires a full-blown parser. Check out [`is-svg`](https://github.com/sindresorhus/is-svg) for something that mostly works.
613+
- `.svg` - Supported by [third-party detector](#available-third-party-file-type-detectors).
601614
602615
#### tokenizer
603616

0 commit comments

Comments
 (0)