-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement local search adapter
- Loading branch information
Showing
7 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {join} from 'path'; | ||
|
||
import {BUNDLE_FOLDER, Lang, RTL_LANGS} from '../constants'; | ||
|
||
import manifest from '@diplodoc/client/manifest'; | ||
import {SEARCH_PAGE_DEPTH} from '../services/search'; | ||
|
||
export function generateStaticSearch(lang: Lang): string { | ||
const isRTL = RTL_LANGS.includes(lang); | ||
|
||
return ` | ||
<!DOCTYPE html> | ||
<html lang="${lang}" dir="${isRTL ? 'rtl' : 'ltr'}"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<meta name="robots" content="noindex"/> | ||
<title>Search</title> | ||
<style type="text/css"> | ||
body { | ||
height: 100vh; | ||
} | ||
</style> | ||
${manifest.search.css | ||
.filter((file: string) => isRTL === file.includes('.rtl.css')) | ||
.map((url: string) => join('../'.repeat(SEARCH_PAGE_DEPTH), BUNDLE_FOLDER, url)) | ||
.map((src: string) => `<link type="text/css" rel="stylesheet" href="${src}" />`) | ||
.join('\n')} | ||
</head> | ||
<body class="g-root g-root_theme_light"> | ||
<div id="root"></div> | ||
${manifest.search.js | ||
.map((url: string) => join('../'.repeat(SEARCH_PAGE_DEPTH), BUNDLE_FOLDER, url)) | ||
.map( | ||
(src: string) => | ||
`<script type="application/javascript" src="${src}"></script>`, | ||
) | ||
.join('\n')} | ||
</body> | ||
</html> | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import type {DocInnerProps, DocPageData} from '@diplodoc/client'; | ||
import type {Lang} from '../constants'; | ||
|
||
import {join} from 'node:path'; | ||
import {mkdirSync, writeFileSync} from 'node:fs'; | ||
|
||
import {Indexer} from '@diplodoc/search-extension/indexer'; | ||
|
||
import {ArgvService} from '.'; | ||
import {generateStaticSearch} from '../pages'; | ||
import {copyFileSync} from 'fs'; | ||
|
||
const api = require.resolve('@diplodoc/search-extension/worker'); | ||
|
||
let indexer: Indexer; | ||
|
||
function init() { | ||
indexer = new Indexer(); | ||
} | ||
|
||
function add(info: DocInnerProps) { | ||
const {search} = ArgvService.getConfig(); | ||
if (!search || search.provider !== 'local') { | ||
return; | ||
} | ||
|
||
const {lang, data, router} = info; | ||
|
||
// TODO: index leading and lpc pages | ||
if (data.leading) { | ||
return; | ||
} | ||
|
||
const base = (data.toc as {base?: string}).base || ''; | ||
const url = base + '/' + router.pathname; | ||
|
||
indexer.add(lang, url, data as DocPageData); | ||
} | ||
|
||
async function release() { | ||
const {search} = ArgvService.getConfig(); | ||
if (!search) { | ||
return; | ||
} | ||
|
||
if (search.provider === 'local') { | ||
copyFileSync(api, apiLink()); | ||
} | ||
|
||
for (const lang of indexer.langs) { | ||
const {index, registry} = await indexer.release(lang); | ||
const dir = outputDir(lang); | ||
|
||
mkdirSync(dir, {recursive: true}); | ||
writeFileSync(indexLink(lang), index, 'utf8'); | ||
writeFileSync(registryLink(lang), registry, 'utf8'); | ||
writeFileSync(pageLink(lang), generateStaticSearch(lang as Lang), 'utf8'); | ||
} | ||
} | ||
|
||
function outputDir(lang: string) { | ||
const {output} = ArgvService.getConfig(); | ||
return join(output, '_search', lang); | ||
} | ||
|
||
function bundleDir() { | ||
const {output} = ArgvService.getConfig(); | ||
return join(output, '_bundle'); | ||
} | ||
|
||
function outputLink(lang: string, file: string) { | ||
const dir = outputDir(lang); | ||
const path = join(dir, file); | ||
|
||
return path; | ||
} | ||
|
||
// <root>/_search/ru | ||
export const SEARCH_PAGE_DEPTH = 2; | ||
|
||
function pageLink(lang: string) { | ||
return outputLink(lang, 'index.html'); | ||
} | ||
|
||
function apiLink() { | ||
const dir = bundleDir(); | ||
const path = join(dir, 'search-api.js'); | ||
|
||
return path; | ||
} | ||
|
||
function indexLink(lang: string) { | ||
return outputLink(lang, 'index.json'); | ||
} | ||
|
||
function registryLink(lang: string) { | ||
return outputLink(lang, 'registry.json'); | ||
} | ||
|
||
function config(lang: string) { | ||
const {search, output} = ArgvService.getConfig(); | ||
|
||
if (!search || search.provider !== 'local') { | ||
return {}; | ||
} | ||
|
||
const short = (link: string, root: string) => link.replace(root, '').replace(/^\/?/, ''); | ||
|
||
return { | ||
api: short(apiLink(), bundleDir()), | ||
link: short(pageLink(lang), output), | ||
resources: { | ||
index: short(indexLink(lang), output), | ||
registry: short(registryLink(lang), output), | ||
}, | ||
}; | ||
} | ||
|
||
export default { | ||
init, | ||
add, | ||
release, | ||
config, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters