Skip to content

Commit e59b732

Browse files
committed
Use @file-type/xml as a detector example
Adds the following to README: - Refer to third-party detectors where SVG is supported - Describe how to add a third party file-type detector - Add a list of third-party detectors
1 parent 8dfed93 commit e59b732

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

core.d.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,27 @@ 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+
```
122137
123-
Custom detectors allow for:
124-
- Introducing new `FileTypeResult` entries.
125-
- Modifying the detection behavior of existing `FileTypeResult` types.
138+
### Available third party file-type detectors
139+
* [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as: GLM, KML, MusicXML, RSS, SVG, XHTML
126140
127141
### Detector execution flow
128142
@@ -131,7 +145,7 @@ If a detector returns `undefined`, the following rules apply:
131145
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
132146
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.
133147
134-
### Example usage
148+
### Example writing a custom detector
135149
136150
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
137151

readme.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,27 @@ 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+
```
351364
352-
Custom detectors allow for:
353-
- Introducing new `FileTypeResult` entries.
354-
- Modifying the detection behavior of existing `FileTypeResult` types.
365+
### Available third party file-type detectors
366+
* [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as: GLM, KML, MusicXML, RSS, SVG, XHTML
355367
356368
### Detector execution flow
357369
@@ -360,7 +372,7 @@ If a detector returns `undefined`, the following rules apply:
360372
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
361373
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.
362374
363-
### Example usage
375+
### Writing your own custom detector
364376
365377
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
366378
@@ -597,7 +609,7 @@ The following file types will not be accepted:
597609
- `.ppt` - Microsoft PowerPoint97-2003 Document
598610
- `.msi` - Microsoft Windows Installer
599611
- `.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.
612+
- `.svg` - Supported by [third party detector](#available-third-party-file-type-detectors).
601613
602614
#### tokenizer
603615

0 commit comments

Comments
 (0)