New search customization API (experimental) โ
External API โ
Custom JavaScript uses the global frontend object. The new API has two functions:
frontend.customizeSearchForm(...)changes the built-in search forms.frontend.customizeSearchResults(...)changes search requests, result data, exports, sorting, and grouping.
The functions take callback functions or objects containing callback functions. The callbacks receive corpus, form, or result data. The form API also provides functions for creating and changing form nodes. Both APIs are experimental.
The existing frontend.customize(...) API still works. Use the new functions for new search customizations.
Type declarations โ
The frontend serves the declaration file at:
${CONTEXT_URL}/js/customization-api/index.d.tsDownload the declaration file from the same frontend version you deploy. Its Vue types are imported from the vue package, so install Vue as a development dependency in your script's project:
npm install --save-dev vue@3For a JavaScript file, enable checking and reference the local declaration file:
// @ts-check
/// <reference path="./blacklab-frontend-customization-api.d.ts" />
frontend.customizeSearchForm(form => {
form.setExtendedAnnotations(['word', 'lemma', 'pos']);
});The declaration file is also available in the .d.ts and .tgz files attached to GitHub releases. A project with package.json can install the package:
npm install --save-dev vue@3 https://github.com/instituutnederlandsetaal/blacklab-frontend/releases/download/5.0.0/blacklab-frontend-customization-api-5.0.0.tgzThen reference it with:
/// <reference types="@instituutnederlandsetaal/blacklab-frontend-customization-api" />The package declares its supported Vue version as a peer dependency. Vue's own types, including their CSS dependencies, remain in that package; the customization declaration imports them without copying them. These imports are only used by the editor and type checker. No separate /// <reference types="vue" /> is needed, and the browser script continues to use the host's frontend.vue.
Types referenced by the API are available as named exports, including Option, OptGroup, and Options. Use them to annotate variables:
/** @type {import('@instituutnederlandsetaal/blacklab-frontend-customization-api').OptGroup} */
const partsOfSpeech = {
label: 'Parts of speech',
options: [{ value: 'noun', label: 'Noun' }, 'verb'],
};Pin the declarations to the deployed frontend version. If a changed script is served from the same URL, add a query string such as custom.search.js?v=2 to avoid a cached copy.
Search forms โ
The form API has two parts:
configureruns before the built-in form nodes are created. Use it to change the built-in fields and defaults in the predifined ways we have builtin support for.customizeruns after the built-in form has been created. Use it to change the form nodes themselves, add custom fields or even add/remove entire form sections, and generally do custom things the standard form configuration options can't express.
A callback passed directly is the same as configure:
frontend.customizeSearchForm(form => {
form.setSimpleAnnotation('word');
form.setExtendedAnnotations(['word', 'lemma', 'pos']);
form.setAnnotationControl('word', 'autocomplete');
form.setMetadataFilters(['author', 'title']);
form.configureWithin({ enabled: false });
});The available configuration functions are:
| Function | What it changes |
|---|---|
setSimpleAnnotation(id) | Annotation used by Simple search. |
setExtendedAnnotations(ids) | Annotations available in Extended search. |
setAnnotationControl(id, control, fieldId?) | Control used for an annotation: auto, text, autocomplete, select, pos, or lexicon. |
configureAdvanced(options) | Advanced search visibility, annotations, and default annotation. |
setMetadataFilters(ids) | Metadata fields used by the standard filters. |
filterMetadataFields(fn) | Metadata fields shown in search and Explore. |
configureWithin(options) | Within elements and attributes. |
configureAlignBy(options) | Align-by elements and default. |
configureExplore(options) | Explore annotations, groups, and corpus metadata groups. |
configureLexicon({ database }) | Lexicon database. |
addSpanFilter(options) | A filter for a span element attribute. |
Unknown annotation and metadata IDs are ignored and logged to the console. If a default is not in the available list, the first available value is used.
filterMetadataFields only affects search fields and Explore. To change metadata fields in result sorting and grouping menus, use includeMetadataField in customizeSearchResults as well.
Span filters โ
addSpanFilter adds a filter for a span attribute. auto uses a select when BlackLab supplies a complete list of values, and a text input otherwise.
frontend.customizeSearchForm(form => {
form.addSpanFilter({
elementName: 'speech',
attributeName: 'speaker',
control: 'auto',
groupId: 'Speakers',
defaultDisplayName: 'Speaker',
});
form.addSpanFilter({
elementName: 'ab',
attributeName: 'verse',
control: 'range',
groupId: 'Chapter and verse',
});
});A span filter requests span data when it is active. It does not automatically add the attribute to the Group By menu. Use includeGroupingSpanAttribute for that.
Span filters created with the legacy search.metadata.createSpanFilter and registered through search.metadata.addCustomTab still appear in the search form. Legacy span filter registrations can also supply labels for Group By.
Changing the form โ
The second phase receives form.graph, which contains the built-in form nodes. Use form.graph to find, remove, replace, or move nodes. Use the constructors on form to create nodes.
frontend.customizeSearchForm({
configure(form) {
form.setMetadataFilters(['place']);
},
customize(form) {
const year = form.metadataMultiFieldRange(
{ id: 'witness-year', defaultDisplayName: 'Witness year' },
{
id: 'witness-year',
groupId: 'Date',
fromField: 'witness_year_from',
toField: 'witness_year_to',
inputType: 'number',
},
);
const filters = form.graph.getContainer(form.ids.sharedFilters());
const tabId = form.ids.filterTab('Date');
const tab = form.graph.getContainer(tabId);
if (tab) tab.addChildren(year);
else filters.prependChild(form.newContainer(tabId, { title: 'Date' }, [year]));
},
});form.ids provides stable IDs for standard nodes. The constructors include annotation, metadata, within-attribute, container, and form constructors. See the declaration file for all arguments. This lets you easily get the standard builtin fields.
All public constructors create detached nodes. newContainer(id, options, children) and newForm(id, options, children) accept an optional array of children, so you can prepare a complete subtree before inserting or replacing it. Construction does not reserve IDs or change the graph. Use graph.replaceNode to replace a node with the same ID, preserving its position in every parent:
const previous = form.graph.getContainer(form.ids.sharedFilters());
if (previous) {
const replacement = form.newContainer(previous.id, { title: 'Filters', layout: 'list' }, previous.children);
form.graph.replaceNode(previous.id, replacement);
}Insertion and replacement validate and register the whole subtree. Once registered, retrieve the node with graph.getContainer(id) or graph.getForm(id) to edit its children. Replaced nodes' descendants remain registered for reuse; graph.pruneDetachedNodes() discards any that are no longer reachable. Form target selection remains an API gap: newForm defaults to ordinary search.
form.corpus is the complete corpus information object, containing all the info about available annotations, metadata, etc. It is read-only. Result callbacks receive the same corpus object.
Built-in and custom field components โ
Inside customize(form), form.components and form.controllers expose the implementations used by the app; no registration or additional runtime download is needed.
| Component | Controllers |
|---|---|
TextField | annotationTextController, filterTextController |
SelectField | annotationSelectController, filterSelectController |
RangeField | filterRangeController |
CheckboxField | filterCheckboxController |
RadioField | filterRadioController |
DateField | filterDateController |
RawCqlField | expertQueryController |
QueryBuilderField | queryBuilderController |
WithinField | withinController |
Use form.newField(id, controller, component, config) to combine them. It uses the same detached construction and type checks as the internal field factory:
frontend.customizeSearchForm({
customize(form) {
const query = form.newField('example/cql', form.controllers.expertQueryController, form.components.RawCqlField, {});
const demo = form.newForm('example', { title: 'CQL search', layout: 'list' }, [query]);
form.graph.getContainer(form.ids.searchFormsContainer())?.prependChild(demo);
},
});RawCqlField edits a plain string, which expertQueryController submits as CQL without escaping its syntax. Its initial state is empty; { hideLabel: true } hides the built-in label. QueryBuilderField takes an options object containing annotation choices, annotation metadata, and an autocomplete callback. WithinField takes span choices in options. See their exported props and state types for the complete contracts.
The controller owns the default state, query output, summaries, and persistence. The component receives the current state as modelValue and emits update:modelValue with a replacement value. Runtime props such as modelValue, htmlId, and update listeners must not be supplied in node configuration. A custom component can reuse a built-in controller if it uses the same state shape. The declarations export FieldComponentProps, FieldController, and the props/state types for each exposed built-in.
frontend.vue supplies the host's defineComponent, defineAsyncComponent, h, ref, and computed. Use these when writing a custom component as plain JavaScript. For a component with a template string, use defineAsyncComponent and await frontend.vue.loadCompiler() inside its loader before returning the component. The compiler downloads only when requested and reuses the host runtime. Form customization registration stays synchronous, so it remains owned by the script that registered it. Vue is installed locally for its types; the deployed script uses the host runtime.
This first set of exports supports ordinary search forms. Further built-ins, custom container renderers, and other form targets remain planned additions.
Search results โ
customizeSearchResults accepts these hooks:
| Hook | What it changes |
|---|---|
withSpans | Sets withspans, or decides from the final BCQL query. |
includeMetadataField | Shows or hides a metadata field in result sorting and grouping menus. |
highlightStyle | Returns none, static, or hover for a capture or relation. |
hitInfoColumn | Adds a result column and returns its text. |
exportDescription | Returns the description used for an export. |
includeExportSpanAttribute | Includes or excludes a span attribute from exports. |
customizeSorting | Changes a sorting option group. |
includeGroupingSpanAttribute | Includes or excludes a span attribute from Group By. |
customizeGrouping | Changes a grouping option group. |
For the include and style hooks, return null to leave the decision to another customization or the default. For withSpans, a callback returns true, false, or null:
frontend.customizeSearchResults({
withSpans: query => (query.includes('within <ab/>') ? true : null),
includeMetadataField: field => field.id !== 'internal_id',
highlightStyle: section => (section.kind === 'relation' ? 'hover' : 'static'),
includeGroupingSpanAttribute: ({ elementName, attributeName }) => (elementName === 'ab' && attributeName === 'chapter' ? true : null),
});Sorting and grouping callbacks receive an option group. Return a replacement group or null to leave it unchanged. The grouping callback also receives translation helpers:
frontend.customizeSearchResults({
customizeSorting(group) {
return {
...group,
options: group.options.filter(option => typeof option === 'string' || option.value !== 'doc:internal_id'),
};
},
customizeGrouping(group, translate) {
return group;
},
});Result callback data โ
Result data passed to callbacks is read-only. The main callback arguments are:
hitInfoColumn.content({ corpus, hit, spans, field, document }): the hit, its tag spans, the annotated field, and the document.hitInfoColumn.visible(overview): result statistics and either hits with documents or hit groups.exportDescription({ corpus, sourceField, targetFields, bcql, summary }): fields and the complete search summary.highlightStyle(section): capture or relation offsets and names.
Metadata and span attribute values are arrays, so select a value explicitly:
frontend.customizeSearchResults({
hitInfoColumn: {
content({ spans, field, document }) {
const verse = spans.find(span => span.tagName === 'ab')?.attributes?.['chapter-verse']?.[0];
const book = document.metadata.bookId?.[0];
return [field.displayName, book, verse].filter(Boolean).join(' ');
},
},
});Multiple scripts โ
Both registration functions return a function that removes the registration:
const unregister = frontend.customizeSearchResults({
withSpans: true,
});
unregister();Callbacks run in registration order. Result selectors use the newest callback first; returning null continues to the next callback and eventually to the default. For functions that always must return a value (e.g. hitInfoColumn or exportDescription), the newest registration is used.
Registrations belong to the script that created them. They are removed when that script is disposed or when the corpus changes. Register customizations synchronously, not from within an asynchronous callback, this may cause errors or cause the customization to be ignored.
Errors in callbacks are logged to the console. Other callbacks still run where possible.
Moving from frontend.customize โ
| Old API | New API |
|---|---|
search.pattern.uiType(...) | customizeSearchForm โ setAnnotationControl(...) |
search.metadata.showField(...) for search fields | customizeSearchForm โ filterMetadataFields(...) |
search.metadata.showField(...) for result menus | customizeSearchResults โ includeMetadataField(...) |
search.within.includeSpan/includeAttribute | customizeSearchForm โ configureWithin(...) |
createSpanFilter(...) + addCustomTab(...) | customizeSearchForm โ addSpanFilter(...) |
search.pattern.shouldAddWithSpans(...) | customizeSearchResults โ withSpans |
results.matchInfoHighlightStyle(...) | customizeSearchResults โ highlightStyle(...) |
customHitInfo(...) | customizeSearchResults โ hitInfoColumn |
results.export.description(...) | customizeSearchResults โ exportDescription(...) |
results.export.includeSpanAttribute(...) | customizeSearchResults โ includeExportSpanAttribute(...) |
group.includeSpanAttribute(...) | customizeSearchResults โ includeGroupingSpanAttribute(...) |
sort.customize(...) | customizeSearchResults โ customizeSorting(...) |
group.customize(...) | customizeSearchResults โ customizeGrouping(...) |
The old API will remain available for the forseeable future, but will eventually be removed. Avoid using an old and a new hook for the same setting. Modern form settings take precedence over old form settings. For result selectors, returning null lets the old behavior decide when no newer callback returns a value. A registered hitInfoColumn or exportDescription replaces the corresponding old callback.
Concordance rendering and article/document-page callbacks are not covered yet; check back later and keep using the old API for those features for now.