Highlighting search term locations in long procedural guides | Mark.js

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About This Article
This article is created using an automated generation workflow powered by generative AI. It references official documentation and static code from instructional materials, organizing operations, observations, and constraints.

Verification Status: Partial cross-checking against official information and implementation. Real-browser execution of the fixed version has not been verified.

In long procedural guides, it is easy to miss which sentence contains the search results. Highlight only the specific terms you are looking for.

Open Live Demo

Terminology to Know First

  • Highlightingis the process of changing the visual appearance of matching sections.

  • Unmarkingis the process of removing previous highlights.

Think Using a Processing Flowchart

Read from left to right. Anticipate how changes to the "search term" affect the "highlighted locations".

flowchart LR
    A["手順書"] --> B["検索語"] --> C["強調箇所"]

Live Demo Operations

Search for "save". Next, search for "check".

How to Read Results

Previous highlights disappear and only the new term stands out.

If the same term appears twice in a single paragraph, two locations are highlighted. Clearing previous styling before applying a new one ensures that changes in the search term are reflected visually.

Key Code Highlights

Apply mark to the target document element and unmark it before searching again.

The implementation can be checked in "Implementation Code" on the demo screen.

When using it for work

Displaying the number of search results makes it possible to distinguish between cases with no results and the state before an operation.

Finding a position within a single document

The demo displays the fixed sentence: "Save changes. Check the backup. Finally, check the save destination." Enter "save" in the first input field and run it to create the document and the search input field. Use the displayed search field to change it to "check". There is no need to press the page-wide run button again. We expect the highlighted sections and count to update in response to the input change.

WordFixed sentence match count
Save2
Check2
Non-existent0

"Save" appears in both "save" and "save destination." Observe that it does not search exclusively for whole words. Also verify that when switching to an absent word, previous highlights do not remain and the count becomes 0. The table counts are the results of counting literal matches of the fixed string using Node.js, and DOM manipulation by the library has not been executed.

Applying styling without breaking the document

mark.js targets a specified DOM element and typically adds mark elements to matching strings. Since you can change the background color and other styles with CSS, it is easy to see which part of the viewed document matched. The tutorial limits the target to a single document area. If the entire page, including the search input field and buttons, is targeted for search, characters in the operation panel may also be styled. It is important to determine the range in advance.

When searching again, use unmark to remove previous styling. The official API guides using a done callback for asynchronous processing. In the tutorial, mark is called after unmark completes, and the count returned by mark's done callback is displayed in the result field. Unlike an implementation that simply stacks new styling, it is easy to reflect changes in the search term on the screen. However, this does not guarantee execution order when inputs occur rapidly. In actual search UIs, consider designs such as process serialization or identifying the latest input so that the completion of older processes does not overwrite newer results.

Search item count and document count are different

The two items this time represent the number of matching locations within the text, not that two documents were found in the search. Separating the process of finding documents in a full-text search system for long instruction manuals from the process of finding words within a single opened document makes it easier for users to understand. The roles become clear by displaying document names in the search results list, highlighting matching locations in the body text, and allowing users to navigate to the next match.

Handling of words containing spaces, multiple words, and matches spanning elements varies depending on mark.js options. Since the current tutorial uses simple fixed Japanese sentences, searches for text split across multiple HTML elements have not been verified. When implementing this in an actual instruction manual, it should also be decided whether to include tables, links, and code blocks in the search. For screen reader users, indicate the number of matches and current position rather than relying solely on color.

Troubleshooting and Security

If search terms exist in the document but are not highlighted, first inspect the target DOM elements and the actual strings. If highlight elements exist but are not visible, check the CSS and color scheme. If previous highlights persist, check whether the next mark operation starts before unmark completes. Designing the interface to separately indicate an empty input state and zero matches is also helpful.

There is no need to insert externally obtained text as raw HTML. The tutorial places fixed text via textContent and targets only that display range. In production, maintain the access permissions for instruction manuals and avoid entering internal documents or confidential search terms into public tutorials. It is also important to remember that the search function itself does not manage access permissions.

Scope of Verification

The specified tutorial version is mark.js 8.11.1. Verified fixed string literal match counts implemented in Node.js 22.16.0: save 2, confirm 2, non-existent 0. mark.js and DOM rendering not executed. CDN acquisition, version matching against license requirements, and actual display must be verified separately.

References

GitHub Sample

Implementation and Experiment Procedures

Document information

Article title
Highlighting search term locations in long procedural guides | Mark.js
Published
Updated
Source
https://papanda925.com/?p=19059&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL