diff --git a/package.json b/package.json index 9812d17..d1bd404 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "typescript": "4.4.4" }, "dependencies": { - "minisearch": "^5.0.0-beta1", - "remove-markdown": "^0.3.0" + "markdown-to-txt": "^2.0.0", + "minisearch": "^5.0.0-beta1" } } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cbce60a..9eb4bdd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,17 +12,17 @@ specifiers: eslint-plugin-import: 2.22.1 eslint-plugin-node: 11.1.0 eslint-plugin-promise: 5.0.0 + markdown-to-txt: ^2.0.0 minisearch: ^5.0.0-beta1 obsidian: latest prettier: ^2.6.2 prettier-eslint: ^13.0.0 - remove-markdown: ^0.3.0 tslib: 2.3.1 typescript: 4.4.4 dependencies: + markdown-to-txt: 2.0.0 minisearch: 5.0.0-beta1 - remove-markdown: 0.3.0 devDependencies: '@types/node': 16.11.26 @@ -1490,10 +1490,18 @@ packages: path-exists: 3.0.0 dev: true + /lodash.escape/4.0.1: + resolution: {integrity: sha1-yQRGkMIeBClL6qUXcS/e0fqI3pg=} + dev: false + /lodash.merge/4.6.2: resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} dev: true + /lodash.unescape/4.0.1: + resolution: {integrity: sha1-vyJJiGzlFM2hEvrpIYzcBlIR/Jw=} + dev: false + /lodash/4.17.21: resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} dev: true @@ -1517,6 +1525,20 @@ packages: yallist: 4.0.0 dev: true + /markdown-to-txt/2.0.0: + resolution: {integrity: sha512-H5DSAY6frUQ6/CR0nJMlGCEuSA3pvi14dYfICJPKLAvd/rDk6eb+cj84zU5XTNYzECyCcujoAL1PHhjmOgeqUw==} + dependencies: + lodash.escape: 4.0.1 + lodash.unescape: 4.0.1 + marked: 3.0.8 + dev: false + + /marked/3.0.8: + resolution: {integrity: sha512-0gVrAjo5m0VZSJb4rpL59K1unJAMb/hm8HRXqasD8VeC8m91ytDPMritgFSlKonfdt+rRYYpP/JfLxgIX8yoSw==} + engines: {node: '>= 12'} + hasBin: true + dev: false + /merge2/1.4.1: resolution: {integrity: sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==} engines: {node: '>= 8'} @@ -1779,10 +1801,6 @@ packages: engines: {node: '>=8'} dev: true - /remove-markdown/0.3.0: - resolution: {integrity: sha1-XktmdJOpNXlyjz1S7MHbnKUF3Jg=} - dev: false - /require-relative/0.8.7: resolution: {integrity: sha1-eZlTn8ngR6N5KPoZb44VY9q9Nt4=} dev: true diff --git a/src/main.ts b/src/main.ts index a3e6d02..b00a3e3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,6 @@ import { Notice, Plugin, SuggestModal, TAbstractFile, TFile } from 'obsidian' import MiniSearch from 'minisearch' -import removeMarkdown from 'remove-markdown' +import { markdownToTxt } from 'markdown-to-txt' type OmniNote = { path: string @@ -61,13 +61,20 @@ export default class OmnisearchPlugin extends Plugin { // Index files that are already present const start = new Date().getTime() const files = this.app.vault.getMarkdownFiles() + + // This is basically the same behavior as MiniSearch's `addAllAsync()`. + // We index files by batches of 10 + console.log('Omnisearch - indexing ' + files.length + ' files') for (let i = 0; i < files.length; ++i) { - if (i % 100 === 0) await wait(100) - await this.addToIndex(files[i]) + if (i % 10 === 0) await wait(0) + const file = files[i] + // console.log(file.path) + await this.addToIndex(file) } + if (files.length > 0) { new Notice( - `Omnisearch - Loaded ${files.length} notes in ${ + `Omnisearch - Indexed ${files.length} notes in ${ new Date().getTime() - start }ms`, ) @@ -232,7 +239,7 @@ class OmnisearchModal extends SuggestModal { // Sort the terms from smaller to larger // and highlight them in the title and body const terms = result.terms.sort((a, b) => a.length - b.length) - const reg = new RegExp(terms.join('|'), 'gi') + const reg = new RegExp(escapeRegex(terms.join('|')), 'gi') body = body.replace(reg, highlighter) title = title.replace(reg, highlighter) name = name.replace(reg, highlighter) @@ -281,7 +288,7 @@ function highlighter(str: string): string { * @param text */ function clearContent(text: string): string { - return removeMarkdown(removeFrontMatter(text)) + return markdownToTxt(removeFrontMatter(text)) } /** @@ -330,3 +337,8 @@ function wait(ms: number): Promise { setTimeout(resolve, ms) }) } + +// https://stackoverflow.com/a/3561711 +function escapeRegex(str: string): string { + return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') +} diff --git a/src/modules.d.ts b/src/modules.d.ts deleted file mode 100644 index c5196e0..0000000 --- a/src/modules.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'remove-markdown'