Skip to content

Commit

Permalink
支持 meilisearch
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
2234839 committed Jan 1, 2024
1 parent f4c08e3 commit 6470701
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 87 deletions.
40 changes: 0 additions & 40 deletions apps/frontend/index.html
Expand Up @@ -16,15 +16,8 @@
y = l.getElementsByTagName(r)[ 0 ]; y.parentNode.insertBefore(t, y)
})(window, document, "clarity", "script", "kdd1bzgo7w");
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@8.1.0/themes/reset-min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@8.1.0/themes/satellite-min.css">
</head>

<body>
<div class="wrapper">
<div id="searchbox" focus></div>
<div id="hits"></div>
</div>
<div id="app"></div>
<script type="module" src="./src/webpage.ts"></script>
</body>
Expand All @@ -35,37 +28,4 @@
padding: 0 10px;
}
</style>
<!-- <script
src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
<script>
const search = instantsearch({
indexName: "llej_blogs",
searchClient: instantMeiliSearch(
"http://zixu.shenzilong.cn", '6e73f23421923228d8060729a97d5a8604d2ae1c00cea384b46f035c7b82706b'
).searchClient
})
search.addWidgets([
instantsearch.widgets.searchBox({
container: "#searchbox"
}),
instantsearch.widgets.configure({ hitsPerPage: 8 }),
instantsearch.widgets.hits({
container: "#hits",
templates: {
item: `
<div>
<div class="hit-name">
<a href="{{#helpers.highlight}}{ "attribute": "url" }{{/helpers.highlight}}">
{{#helpers.highlight}}{ "attribute": "title" }{{/helpers.highlight}}
</a>
</div>
</div>
`
}
})
])
search.start();
</script> -->

</html>
4 changes: 2 additions & 2 deletions apps/frontend/package.json
@@ -1,7 +1,7 @@
{
"name": "frontend",
"private": true,
"version": "0.0.8",
"version": "0.0.9",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -14,7 +14,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.478.0",
"@hono/node-server": "^1.3.3",
"cheerio": "^1.0.0-rc.12",
"cheerio": "1.0.0-rc.12",
"commander": "^11.1.0",
"hono": "^3.11.8",
"jszip": "^3.10.1",
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/src/core/build.ts
Expand Up @@ -288,6 +288,7 @@ ${(
if (config.compressedZip) {
yield `=== 开始生成压缩包 ===`
await downloadZIP(fileTree, {
// TODO 这里应该移出来成为全局的写选项
withoutZip: config.withoutPublicZip,
publicZip: config.cdn.publicZip,
})
Expand Down
20 changes: 8 additions & 12 deletions apps/frontend/src/pages/config_tab.tsx
Expand Up @@ -136,18 +136,14 @@ export default defineComponent({
show-count
clearable
:allow-input={this.validateInput}
v-slots={{
footer: () => (
<NButton
type="primary"
onClick={this.add}
disabled={!this.validateInput(this.name)}
>
确认
</NButton>
),
}}
/>

/><br /><br />
<NButton
type="primary"
onClick={()=>this.add()}
disabled={!this.validateInput(this.name)}>
确认
</NButton><br />
不能以双下划线(__)开头、也不能为空、不能和已有的配置项重名
</NCard>
</NModal>
Expand Down
90 changes: 58 additions & 32 deletions apps/frontend/src/plugins/meilisearch_plugin/meilisearch_upload.ts
@@ -1,6 +1,19 @@
import { Meilisearch, Index } from 'meilisearch'
import { OceanPressPlugin } from '~/core/ocean_press.ts'

import { load } from 'cheerio'
type doc = {
id: string
title?: string
content: string
url: string
lvl0?: string
lvl1?: string
lvl2?: string
lvl3?: string
lvl4?: string
lvl5?: string
lvl6?: string
}
export class MeilisearchPlugin implements OceanPressPlugin {
_melisearch: Meilisearch | undefined
_index: Index | undefined
Expand Down Expand Up @@ -28,38 +41,51 @@ export class MeilisearchPlugin implements OceanPressPlugin {
}
return this._index!
}
async addDocument() {
docs: { [id: string]: doc } = {}
async addDocument(doc: doc) {
this.docs[doc.id] = doc
}
async updateDocument() {
console.log(`开始上传数据到 ${this.host}`)
const index = await this._getIndex()
await index.addDocuments([
{
id: 'test',
title: '标题名啊',
content: '内容啊',
url: '/崮生',
lvl0: '笔记',
},
{
id: 'test2',
title: 'test2',
content: 'test2',
url: '/test2',
lvl0: '笔记',
lvl1: '测试',
},
{
id: 'test3',
title: 'test2',
content: 'test2',
url: '/test2',
lvl0: '笔记',
lvl1: '测试',
lvl3: '测试lvl3',
},
])
const res = await index.addDocuments(Object.values(this.docs))
console.log(`上传结果`, res)
}
build_renderHTML: OceanPressPlugin['build_renderHTML'] = async (c, next) => {
console.log(`${this._indexName} 插件拦截成功`, c[0]?.ID)
// 在这里上传数据到搜索引擎,并且可以添加对应的js代码引入
return next(...c)
build_onFileTree: OceanPressPlugin['build_onFileTree'] = (c, next) => {
const [tree] = c
console.log('开始生成 meilisearch 所需数据结构')

const htmlTree = Object.keys(tree)
.filter((path) => path.endsWith('.html'))
.map((path) => [path, tree[path]] as const)
for (const [path, html] of htmlTree) {
const $ = load(html.toString())
const entries = $('.h1,.h2,.h3,.h4,.h5,.h6,.p').toArray()
const level: Record<string, string> = { lvl0: $('title').text() }
for (const el of entries) {
/** h1~h6、p */
const c = el.attribs.class
if (c !== 'p') {
Object.keys(level).forEach((lv) => {
if (lv.substring(3, 4) > c.substring(1, 2)) {
/** 跳出层级 */
delete level[lv]
}
})
/** 进入层级 */
level[`lvl${c.substring(1, 2)}`] = $(el).text()
}
this.addDocument({
id: el.attribs.id,
content: $(el).text(),
url: `${path}#${el.attribs.id}`,
...level,
})
}

if (path.endsWith('index.html')) break
}
this.updateDocument()
return next(tree)
}
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6470701

Please sign in to comment.