一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
47 行
1.7 KiB
TypeScript
47 行
1.7 KiB
TypeScript
import { htmlentities } from '@joplin/utils/html';
|
|
const Mark = require('mark.js/dist/mark.min.js');
|
|
const markJsUtils = require('@joplin/lib/markJsUtils');
|
|
import { replaceRegexDiacritics, pregQuote } from '@joplin/lib/string-utils';
|
|
import { HighlightedWord } from '@joplin/lib/reducer';
|
|
|
|
const getNoteTitleHtml = (highlightedWords: HighlightedWord[], displayTitle: string) => {
|
|
if (highlightedWords.length) {
|
|
const titleElement = document.createElement('span');
|
|
titleElement.textContent = displayTitle;
|
|
const mark = new Mark(titleElement, {
|
|
exclude: ['img'],
|
|
acrossElements: true,
|
|
});
|
|
|
|
mark.unmark();
|
|
|
|
try {
|
|
for (const wordToBeHighlighted of highlightedWords) {
|
|
markJsUtils.markKeyword(mark, wordToBeHighlighted, {
|
|
pregQuote: pregQuote,
|
|
replaceRegexDiacritics: replaceRegexDiacritics,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
if (error.name !== 'SyntaxError') {
|
|
throw error;
|
|
}
|
|
// An error of 'Regular expression too large' might occur in the markJs library
|
|
// when the input is really big, this catch is here to avoid the application crashing
|
|
// https://github.com/laurent22/joplin/issues/7634
|
|
// console.error('Error while trying to highlight words from search: ', error);
|
|
}
|
|
|
|
// Note: in this case it is safe to use dangerouslySetInnerHTML because titleElement
|
|
// is a span tag that we created and that contains data that's been inserted as plain text
|
|
// with `textContent` so it cannot contain any XSS attacks. We use this feature because
|
|
// mark.js can only deal with DOM elements.
|
|
// https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
|
|
return titleElement.outerHTML;
|
|
} else {
|
|
return htmlentities(displayTitle);
|
|
}
|
|
};
|
|
|
|
export default getNoteTitleHtml;
|