plain_search_index_filter
The Plain Search Index Filter module introduces a powerful Twig extension, strip_tags_safe, designed to enhance the rendering of search index results in Drupal by seamlessly removing unwanted HTML while preserving the spacing and integrity of text. This solution prevents word merging issues commonly seen with the standard striptags filter, making it ideal for ensuring clean, readable search results.
Features
- Basic Functionality: The module provides a new Twig filter strip_tags_safe, which can be applied to render arrays and strings to remove all HTML tags while preventing the merging of adjacent words.
- Unique Features: Unlike striptags, strip_tags_safe maintains spaces where HTML tags were, ensuring that words do not run together.
- Use Cases: Ideal for sites where search indexes need clean text inputs free from HTML clutter, such as academic databases, corporate websites, and libraries.
New Features
Configuration Page
- Added a settings page at Administration > Configuration > System > Plain Search Index Filter settings
- Configure link now available on the Extend page for quick access
Allowed HTML Tags
- New option to preserve specific HTML tags when stripping content
- Add multiple tags using the "Add another tag" interface
- Useful for keeping semantic markup like
<a>,<strong>,<em>in indexed content
Convert Relative Links to Absolute URLs
- New checkbox option to convert relative URLs to absolute
- Converts href="/path" and src="/path" attributes to include the full domain
- Ideal for content being indexed by external search systems
Post-Installation
After installing the Plain Search Index Filter module:
- No additional configuration page is needed.
- Simply apply the strip_tags_safe filter within your Twig templates to sanitize output for search indexes or any other text display.
- Example usage in node--search-index.html.twig:
{{% set renderContent %} {{ content }} {% endset %} {{ renderContent|strip_tags_safe }}
Additional Requirements
For more information about setting up your Search API index using rendered HTML see this page.
Recommended modules/libraries
- Search API: Complements search functionalities, particularly when creating custom search pages and indexes.
Similar projects
While the twig's |striptags filter performs a similar function, the Plain Search Index Filter module uniquely preserves text spacing and integrity, making it more suitable for professional and academic applications where text readability is crucial.