Layer 1: Build-Time Index Generation
The search plugin uses an HTML-first architecture, processing final rendered HTML pages using Cheerio for accurate content indexing. This ensures the search index reflects exactly what users see in their browsers.
Installing the Plugin
For Metalsmith:
npm install metalsmith-search
For Eleventy:
npm install eleventy-plugin-search
Configuration
Critical: The search plugin must run AFTER layouts/templates in your pipeline to process rendered HTML.
Metalsmith (metalsmith.js):
import search from 'metalsmith-search';
import layouts from '@metalsmith/layouts';
metalsmith
.use(collections({
blog: {
pattern: 'blog/*.md',
sortBy: 'card.date',
reverse: true
}
}))
.use(layouts({
directory: 'lib/layouts',
transform: 'nunjucks'
}))
.use(search({
pattern: '**/*.html',
excludeSelectors: ['nav', 'header', 'footer'],
ignore: [
'**/search.md',
'**/search-index.json'
]
}))
Eleventy (eleventy.config.js):
import searchPlugin from 'eleventy-plugin-search';
export default function(eleventyConfig) {
eleventyConfig.addPlugin(searchPlugin, {
pattern: '**/*.html',
excludeSelectors: ['nav', 'header', 'footer'],
ignore: ['**/search/**', '**/search-index.json'],
outputPath: '/search-index.json'
});
}
Both plugins process HTML files after layout rendering, using Cheerio to parse the DOM and extract text content. This HTML-first approach ensures accurate indexing of the actual page content users will see.