Plugin Configuration
The search component requires the metalsmith-search
plugin to be properly configured in metalsmith.js
build file.
Basic Plugin Installation
import search from 'metalsmith-search';
const metalsmith = Metalsmith(__dirname)
.source('src')
.destination('build')
.use(search({
indexLevels: ['page', 'section']
}))
.use(layouts())
.use(collections())
.build((err) => {
if (err) throw err;
console.log('Build complete with search index!');
});
Plugin Position
Place the search plugin before templating is applied for optimal content extraction
Configuration Options
File Processing:
Property | Type | Default | Description |
---|
pattern | string | '**/*.md' | Files to index |
ignore | array | ['**/search.md'] | Exclude specific files |
Index Configuration:
Property | Type | Default | Description |
---|
indexPath | string | 'search-index.json' | Output file path |
indexLevels | array | ['page', 'section'] | Content levels to index |
sectionsField | string | 'sections' | Component array field name |
Component Processing:
Property | Type | Default | Description |
---|
autoDetectSectionTypes | boolean | true | Auto-discover component types |
sectionTypeField | string | 'sectionType' | Component type field |
Content Processing:
Property | Type | Default | Description |
---|
stripHtml | boolean | true | Remove HTML markup |
generateAnchors | boolean | true | Create section anchors |
maxSectionLength | number | 2000 | Split long sections (characters) |
chunkSize | number | 1500 | Target chunk size (characters) |
minSectionLength | number | 50 | Skip tiny sections (characters) |
processMarkdownFields | boolean | true | Process markdown in frontmatter |
frontmatterFields | array | ['summary', 'intro', 'leadIn'] | Fields to process |
Performance Options:
Property | Type | Default | Description |
---|
batchSize | number | 10 | Process files in batches (file count) |
async | boolean | false | Enable for very large sites |
Complete Example:
.use(search({
pattern: '**/*.md',
ignore: ['**/search.md'],
indexPath: 'search-index.json',
indexLevels: ['page', 'section'],
sectionsField: 'sections',
autoDetectSectionTypes: true,
sectionTypeField: 'sectionType',
stripHtml: true,
generateAnchors: true,
maxSectionLength: 2000,
chunkSize: 1500,
minSectionLength: 50,
processMarkdownFields: true,
frontmatterFields: ['summary', 'intro', 'leadIn'],
fuseOptions: {
keys: [
{ name: 'title', weight: 10 },
{ name: 'tags', weight: 8 },
{ name: 'leadIn', weight: 5 },
{ name: 'prose', weight: 3 },
{ name: 'content', weight: 1 }
],
threshold: 0.3,
includeScore: true,
includeMatches: true,
minMatchCharLength: 3
},
batchSize: 10,
async: false
}))