diff --git a/SearchBox.vue b/SearchBox.vue deleted file mode 100644 index ce8b4a9..0000000 --- a/SearchBox.vue +++ /dev/null @@ -1,364 +0,0 @@ - - - - - diff --git a/example/docs/.vuepress/config.js b/example/docs/.vuepress/config.js index 9bcbb53..b7c01bd 100755 --- a/example/docs/.vuepress/config.js +++ b/example/docs/.vuepress/config.js @@ -1,10 +1,10 @@ -const { description } = require('../../package') +const { description } = require("../../package"); module.exports = { /** * Ref:https://v1.vuepress.vuejs.org/config/#title */ - title: 'Vuepress Docs Boilerplate', + title: "Vuepress Docs Boilerplate", /** * Ref:https://v1.vuepress.vuejs.org/config/#description */ @@ -16,9 +16,12 @@ module.exports = { * ref:https://v1.vuepress.vuejs.org/config/#head */ head: [ - ['meta', { name: 'theme-color', content: '#3eaf7c' }], - ['meta', { name: 'apple-mobile-web-app-capable', content: 'yes' }], - ['meta', { name: 'apple-mobile-web-app-status-bar-style', content: 'black' }] + ["meta", { name: "theme-color", content: "#3eaf7c" }], + ["meta", { name: "apple-mobile-web-app-capable", content: "yes" }], + [ + "meta", + { name: "apple-mobile-web-app-status-bar-style", content: "black" }, + ], ], /** @@ -27,45 +30,40 @@ module.exports = { * ref:https://v1.vuepress.vuejs.org/theme/default-theme-config.html */ themeConfig: { - repo: '', + repo: "", editLinks: false, - docsDir: '', - editLinkText: '', + docsDir: "", + editLinkText: "", lastUpdated: false, nav: [ { - text: 'Guide', - link: '/guide/', + text: "Guide", + link: "/guide/", }, { - text: 'Config', - link: '/config/' + text: "Config", + link: "/config/", }, { - text: 'VuePress', - link: 'https://v1.vuepress.vuejs.org' - } + text: "VuePress", + link: "https://v1.vuepress.vuejs.org", + }, ], sidebar: { - '/guide/': [ + "/guide/": [ { - title: 'Guide', + title: "Guide", collapsable: false, - children: [ - '', - 'using-vue', - ] - } + children: ["", "using-vue"], + }, ], - } + }, }, /** * Apply plugins,ref:https://v1.vuepress.vuejs.org/zh/plugin/ */ plugins: [ - '@vuepress/plugin-back-to-top', - '@vuepress/plugin-medium-zoom', - require.resolve('../../../') - ] -} + [require.resolve("../../../"), {}], + ], +}; diff --git a/example/docs/config/README.md b/example/docs/config/README.md index 63a04b9..f05e8e2 100755 --- a/example/docs/config/README.md +++ b/example/docs/config/README.md @@ -2,8 +2,6 @@ sidebar: auto --- -# Config - ## foo - Type: `string` diff --git a/index.js b/index.js index cc6597a..db1bec6 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,31 @@ -const { path } = require('@vuepress/shared-utils') -const he = require('he'); +const { path } = require("@vuepress/shared-utils"); +const { getPageText } = require("./src/utils"); + +let defaultSearchOptions = { + encode: "icase", + tokenize: "forward", + resolution: 9, + doc: { + id: "key", + field: ["title", "content", "headers"], + }, +}; + +const DEFAULT_SEARCH_RESULT_LENGTH = 60; module.exports = (options) => ({ - extendPageData ($page) { - if (!$page._strippedContent) { - return - } - // _strippedContent does not contain the YAML frontmatter - const { html } = $page._context.markdown.render($page._strippedContent) - const text = he.decode( // decode HTML entities like " - html - .replace(/(<[^>]+>)+/g, " ") // remove HTML tags - .replace(/^\s*#\s/gm, "") // remove header anchors inserted by vuepress - ) - $page.content = text - }, - alias: { - '@SearchBox': - path.resolve(__dirname, 'SearchBox.vue') - }, -}) + extendPageData($page) { + $page.content = getPageText($page); + }, + alias: { + "@SearchBox": path.resolve(__dirname, "src", "SearchBox.vue"), + }, + define: { + SEARCH_OPTIONS: options.search_options || defaultSearchOptions, + SEARCH_MAX_SUGGESTIONS: options.maxSuggestions || 10, + SEARCH_PATHS: options.searchPaths || null, + SEARCH_HOTKEYS: options.searchHotkeys || "s", + SEARCH_RESULT_LENGTH: + Number(options.searchResultLength) || DEFAULT_SEARCH_RESULT_LENGTH, + }, +}); diff --git a/src/SearchBox.vue b/src/SearchBox.vue new file mode 100644 index 0000000..8d9e552 --- /dev/null +++ b/src/SearchBox.vue @@ -0,0 +1,360 @@ + + + + + diff --git a/search.svg b/src/assets/search.svg similarity index 100% rename from search.svg rename to src/assets/search.svg diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..cb166bc --- /dev/null +++ b/src/utils.js @@ -0,0 +1,40 @@ +const he = require("he"); + +/** + * @param page + * @returns {string} + */ +module.exports.getPageText = (page) => { + if (!page._strippedContent) { + return ""; + } + // _strippedContent does not contain the YAML frontmatter + const { html } = page._context.markdown.render(page._strippedContent); + + const text = he.decode( + // decode HTML entities like " + html + .replace(/(<[^>]+>)+/g, " ") // remove HTML tags + .replace(/^\s*#\s/gm, "") // remove header anchors inserted by vuepress + ); + return text; +}; + +/** + * @param {string} fullText + * @param {string} highlightTarget + * @returns {string} + */ +module.exports.highlightText = (fullText, highlightTarget) => { + let result = fullText; + highlightWords = highlightTarget.split(" ").filter((word) => word.length > 0); + if (highlightWords.length > 0) { + for (const word of highlightWords) { + result = result.replace(new RegExp(word, "ig"), "$&"); + } + } else { + result = fullText.replace(new RegExp(highlightTarget, "ig"), "$&"); + } + + return result; +};