Skip to content

Commit

Permalink
feat: adds Vitepress plugin (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva authored Dec 6, 2023
1 parent e5d060d commit 79ec043
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 25 deletions.
7 changes: 4 additions & 3 deletions packages/docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from 'vitepress'
import { OramaSearch } from '../plugins/searchbox/index.ts'
import { OramaPlugin } from '@orama/plugin-vitepress'

// https://vitepress.dev/reference/site-config
export default defineConfig({
Expand All @@ -24,7 +24,7 @@ export default defineConfig({
},

vite: {
plugins: [OramaSearch()]
plugins: [OramaPlugin()]
},

rewrites: {
Expand Down Expand Up @@ -97,10 +97,11 @@ export default defineConfig({
items: [
{ text: 'Introduction', link: '/open-source/plugins/introduction.html' },
{ text: 'Writing your own plugins', link: '/open-source/plugins/writing-your-own-plugins.html' },
{ text: 'Plugin Vitepress', link: '/open-source/plugins/plugin-vitepress.html' },
{ text: 'Plugin Docusaurus', link: '/open-source/plugins/plugin-docusaurus.html' },
{ text: 'Plugin Telemetry', link: '/open-source/plugins/plugin-telemetry.html' },
{ text: 'Plugin Astro', link: '/open-source/plugins/plugin-astro.html' },
{ text: 'Plugin Data Persistence', link: '/open-source/plugins/plugin-data-persistence.html' },
{ text: 'Plugin Docusaurus', link: '/open-source/plugins/plugin-docusaurus.html' },
{ text: 'Plugin Match Highlight', link: '/open-source/plugins/plugin-match-highlight.html' },
{ text: 'Plugin Parsedoc', link: '/open-source/plugins/plugin-parsedoc.html' },
{ text: 'Plugin Nextra', link: '/open-source/plugins/plugin-nextra.html' },
Expand Down
1 change: 0 additions & 1 deletion packages/docs/open-source/plugins/plugin-nextra.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ outline: deep
# Plugin Nextra

[Nextra](https://nextra.site) is a static site generator for Next.js, which provides an easy and effective way to create a blog or a documentation website.
The website you're visiting right now is built with Nextra!

Orama provides its own official plugin to be integrated with Nextra.

Expand Down
37 changes: 37 additions & 0 deletions packages/docs/open-source/plugins/plugin-vitepress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
outline: deep
---

# Plugin Vitepress

Vitepress is a Vite & Vue powered static site generator.

The website you're visiting right now is built with Vitepress and uses the official Orama Vitepress Plugin for search!

## Installation

You can install the plugin using any major Node.js package manager:

```bash copy
npm install @orama/plugin-vitepress
```

## Usage

This plugin will look for all the `.md` files in your documentation directory and will automatically index them for you.

After the installation via the package manager of your choice, you can import the plugin in your `.vitepress/config.js` file:

```js
import { OramaPlugin } from '@orama/plugin-nextra'

export default {
// ...
vite: {
plugins: [OramaPlugin()],
},
// ...
}
```

And that's it! The Orama plugin will do the rest for you.
4 changes: 4 additions & 0 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@orama/orama": "workspace:*",
"@orama/plugin-data-persistence": "workspace:*",
"@orama/plugin-vitepress": "workspace:*",
"@orama/searchbox": "1.0.0-beta.9",
"@vitejs/plugin-vue": "^4.5.1",
"jsdom": "^23.0.1",
Expand All @@ -24,5 +25,8 @@
"@types/jsdom": "^21.1.6",
"@types/markdown-it": "^13.0.7",
"vitepress": "1.0.0-rc.31"
},
"peerDependencies": {
"vue": "^3.0.0"
}
}
30 changes: 30 additions & 0 deletions packages/plugin-vitepress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Vitepress Plugin

[![Tests](https://github.com/oramasearch/orama/actions/workflows/turbo.yml/badge.svg)](https://github.com/oramasearch/orama/actions/workflows/turbo.yml)

Official plugin to provide search capabilities through Orama on any Vitepress website!

# Usage

For the complete usage guide, please refer to the [official plugin documentation](https://docs.oramasearch.com/open-source/plugins/plugin-vitepress).

# TL;DR

```js
// .vitepress/config.js

import { defineConfig } from 'vitepress'
import { OramaPlugin } from '@orama/plugin-vitepress'

export default defineConfig({
// ...
vite: {
plugins: [OramaPlugin()]
},
// ...
})
```

# License

[Apache-2.0](/LICENSE.md)
60 changes: 60 additions & 0 deletions packages/plugin-vitepress/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@orama/plugin-vitepress",
"version": "2.0.0-beta.8",
"description": "Vitepress plugin for local search powered by orama",
"keywords": [
"orama",
"vite",
"vitepress",
"vue"
],
"license": "Apache-2.0",
"main": "./dist/index.js",
"type": "module",
"bugs": {
"url": "https://github.com/oramasearch/orama/issues"
},
"homepage": "https://github.com/oramasearch/orama#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/oramasearch/orama.git"
},
"sideEffects": false,
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "swc --delete-dir-on-start --extensions .ts -d dist src",
"postbuild": "tsc -p . --emitDeclarationOnly && cp ./src/Search.vue ./dist/Search.vue",
"lint": "eslint src --ext .js,.ts,.cts"
},
"dependencies": {
"@orama/orama": "workspace:*",
"@orama/plugin-data-persistence": "workspace:*",
"@orama/searchbox": "1.0.0-beta.9",
"@vitejs/plugin-vue": "^4.5.1",
"jsdom": "^23.0.1",
"markdown-it": "^13.0.2",
"slugify": "^1.6.6",
"vite": "^4.1.4"
},
"publishConfig": {
"access": "public"
},
"lint-staged": {
"*.{ts, tsx}": "eslint ./src --cache --fix"
},
"devDependencies": {
"@swc/cli": "^0.1.59",
"@swc/core": "^1.3.27",
"@preact/signals-core": "^1.5.0",
"@types/jsdom": "^21.1.6",
"@types/markdown-it": "^13.0.7",
"typescript": "^5.0.0",
"vitepress": "1.0.0-rc.31"
},
"peerDependencies": {
"vue": "^3.0.0"
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const md = new MarkdownIt({
html: true
})

export async function createOramaContentLoader (paths: string[], root: string) {
async function createOramaContentLoader (paths: string[], root: string) {
const contents = paths
.map(file => ({
path: file.replace(root, '').replace('.md', ''),
Expand All @@ -41,7 +41,6 @@ export async function createOramaContentLoader (paths: string[], root: string) {
schema: presets.docusaurus.schema
})

console.log('Inserting into Orama')
// @ts-ignore
await insertMultiple(db, contents)

Expand Down Expand Up @@ -80,23 +79,21 @@ function parseHTMLContent({ html, path }: { html: string, path: string }): Array
function formatForOrama(data: Array<ParserResult>): Array<OramaSchema> {
try {
const firstH1Header = data.find(section => section.header === 'h1')
const isOSS = data?.[0]?.path?.startsWith('/open-source')

return data.map((res) => ({
title: res.title,
content: res.content,
section: firstH1Header!.title.replace(/\s$/, ''),
path: res?.path + '#' + slugify.default(res.title, { lower: true }),
category: isOSS ? 'open-source' : 'cloud',
category: ''
}))
} catch (error) {
console.error(error)
return []
}
}

export function OramaSearch(): Plugin {

export function OramaPlugin(): Plugin {
let resolveConfig: any
const virtualModuleId = "virtual:search-data";
const resolvedVirtualModuleId = `\0${virtualModuleId}`
Expand Down Expand Up @@ -160,7 +157,7 @@ export function OramaSearch(): Plugin {
}
}

export const pluginSiteConfig: Partial<SiteConfig> = {
const pluginSiteConfig: Partial<SiteConfig> = {
buildEnd(ctx) {},
transformHead(ctx) {
return []
Expand Down
20 changes: 20 additions & 0 deletions packages/plugin-vitepress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"allowJs": true,
"target": "ES5",
"module": "NodeNext",
"outDir": "dist",
"jsx": "react",
"noImplicitAny": false,
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"esModuleInterop": true,
"declaration": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"sourceMap": true,
"moduleResolution": "nodenext"
},
"include": ["src/*.ts"]
}
Loading

0 comments on commit 79ec043

Please sign in to comment.