feat: SearchQuery.filterResult, an optional filter on results #5

Closed
massifrg wants to merge 1 commit from filterResult into main
massifrg commented 2024-06-25 10:52:45 +02:00 (Migrated from github.com)

This should be a more conformant version of this PR.

I added an optional filter on results, that work the same way of checkWordBoundary.

In particular I wanted to search for a text that also has some Marks.

I changed the internal checkResult function -- originally intended only for the wholeWord option -- this way:

  checkResult(state: EditorState, result: SearchResult) {
    let ok = this.wholeWord ? checkWordBoundary(state, result.from) && checkWordBoundary(state, result.to) : true
    if (!ok) return false
    if (this.filterResult) ok = this.filterResult(state, result)
    return ok
  }

This are two filters that I use to check if the searched text has one or all the Marks in a set:

const allMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => {
  if (marks) {
    const { from, to } = result
    return marks.every(mark => state.doc.rangeHasMark(from, to, mark))
  }
  return true
}

const oneOfMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => {
  if (marks) {
    const { from, to } = result
    return !!marks.find(mark => state.doc.rangeHasMark(from, to, mark))
  }
  return true
}

I've also added a test.

This should be a more conformant version of [this PR](https://github.com/ProseMirror/prosemirror-search/pull/4). I added an optional filter on results, that work the same way of `checkWordBoundary`. In particular I wanted to search for a text that also has some `Mark`s. I changed the internal `checkResult` function -- originally intended only for the `wholeWord` option -- this way: ```ts checkResult(state: EditorState, result: SearchResult) { let ok = this.wholeWord ? checkWordBoundary(state, result.from) && checkWordBoundary(state, result.to) : true if (!ok) return false if (this.filterResult) ok = this.filterResult(state, result) return ok } ``` This are two filters that I use to check if the searched text has one or all the `Mark`s in a set: ```ts const allMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => { if (marks) { const { from, to } = result return marks.every(mark => state.doc.rangeHasMark(from, to, mark)) } return true } const oneOfMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => { if (marks) { const { from, to } = result return !!marks.find(mark => state.doc.rangeHasMark(from, to, mark)) } return true } ``` I've also added a test.
marijnh commented 2024-06-25 12:18:09 +02:00 (Migrated from github.com)

Merged as b285dc6105 and tweaked a bit in 91463ed1b1

Merged as b285dc6105a3be1a3b29113e0364a52a97936654 and tweaked a bit in 91463ed1b1e9e166688f6cb186e968e9dcacc3e7

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
prosemirror/prosemirror-search!5
No description provided.