Skip to content

Create regexHighlight.md#2

Open
orchid-hybrid wants to merge 1 commit intocontextscript:masterfrom
orchid-hybrid:master
Open

Create regexHighlight.md#2
orchid-hybrid wants to merge 1 commit intocontextscript:masterfrom
orchid-hybrid:master

Conversation

@orchid-hybrid
Copy link

Created a context script to quickly highlight and count every occurence of a regex in a page. Example: 'regex \d+' to find all numbers.

Created a context script to quickly highlight and count every occurence of a regex in a page. Example: 'regex \d+' to find all numbers.
@nathanathan
Copy link
Member

I noticed that when I try to do a second regex search (which is pretty common because I almost never get regexs right on my first try), there is no way to clear the highlights from the first search, and matches cannot be made when they overlap highlights spans or links. For example, "regex hello world" won't highlight "hello world".

@orchid-hybrid
Copy link
Author

That's a good point, I am not sure what the best way to enable that would be: maybe regex hilights could be given a special id and before it performs the regex it could remove all spans of that id?

@nathanathan
Copy link
Member

One way to do it would be to use a css class to apply the highlight style and remove the class from all the highlight elements when new highlights are applied. However, the spans would still be present in the HTML so you would need to alter the regex matching code to work across multiple HTML elements. I think that would require applying the regex to the raw text extracted then look up the corresponding elements to highlight in nodeMappings based on the character offsets of the matches. For example:

// To determine the offsets:
myRe = new RegExp(...);
matches = [];
text.replace(myRe, (match, offset)=>{
  matches.push({
    start: offset,
    end: offset + match.length,
    text: match
  });
  // This doesn't actually replace anything.
  return match;
});
// To find the elements that need highlighting:
_.where(nodeMappings, ({start, end})=>{
  return (match_start >= start && match_start < end);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants