Skip to content

Commit

Permalink
feat: 新增 content-update 插件
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Dec 28, 2023
1 parent 90465d2 commit 496bbae
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 35 deletions.
21 changes: 21 additions & 0 deletions plugins/plugin-content-update/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (C) 2021 - PRESENT by pengzhanbo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
21 changes: 21 additions & 0 deletions plugins/plugin-content-update/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# `@vuepress-plume/plugin-content-update`

替换 `@vuepress/client``<Content />` 组件,注入 `onContentUpdated` 生命周期。
实现当页面内容发生更新时,触发 `onContentUpdated` 事件。

## Install
```
yarn add @vuepress-plume/plugin-content-update
```
## Usage
``` js
// .vuepress/config.js
const { contentUpdatePlugin } = require('@vuepress-plume/plugin-content-update')
module.exports = {
// ...
plugins: [
contentUpdatePlugin()
]
// ...
}
```
49 changes: 49 additions & 0 deletions plugins/plugin-content-update/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@vuepress-plume/plugin-content-update",
"type": "module",
"version": "1.0.0-rc.6",
"description": "The Plugin for VuePres 2",
"author": "pengzhanbo <volodymyr@foxmail.com>",
"license": "MIT",
"homepage": "https://github.com/pengzhanbo/vuepress-theme-plume#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/pengzhanbo/vuepress-theme-plume.git"
},
"bugs": {
"url": "https://github.com/pengzhanbo/vuepress-theme-plume/issues"
},
"exports": {
".": "./lib/node/index.js",
"./client": "./lib/client/index.js",
"./package.json": "./package.json"
},
"main": "lib/node/index.js",
"types": "./lib/node/index.d.ts",
"files": [
"lib"
],
"scripts": {
"build": "pnpm run clean && pnpm run copy && pnpm run ts",
"clean": "rimraf lib *.tsbuildinfo",
"copy": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png}\" lib",
"ts": "tsc -b tsconfig.build.json"
},
"dependencies": {
"@vuepress/client": "2.0.0-rc.0",
"@vuepress/core": "2.0.0-rc.0",
"@vuepress/shared": "2.0.0-rc.0",
"@vuepress/utils": "2.0.0-rc.0",
"vue": "^3.3.13",
"vue-router": "4.2.5"
},
"publishConfig": {
"access": "public"
},
"keyword": [
"VuePress",
"vuepress plugin",
"content-update",
"vuepress-plugin-plugin-content-update"
]
}
11 changes: 11 additions & 0 deletions plugins/plugin-content-update/src/client/clientConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineClientConfig } from '@vuepress/client'
import { Content } from './components/Content.js'

export default defineClientConfig({
enhance({ app }) {
if (app._context.components.Content)
delete app._context.components.Content

app.component('Content', Content)
},
})
42 changes: 42 additions & 0 deletions plugins/plugin-content-update/src/client/components/Content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { pagesComponents } from '@internal/pagesComponents'
import { computed, defineComponent, h } from 'vue'
import { usePageData } from '@vuepress/client'
import { runCallbacks } from '../composables/index.js'

declare const __VUEPRESS_DEV__: boolean

/**
* Markdown rendered content
*/
export const Content = defineComponent({

name: 'Content',

props: {
pageKey: {
type: String,
required: false,
default: '',
},
},

setup(props) {
const page = usePageData()
const pageComponent = computed(
() => pagesComponents[props.pageKey || page.value.key],
)
return () =>
pageComponent.value
? h(pageComponent.value, {
onVnodeMounted: runCallbacks,
onVnodeUpdated: runCallbacks,
onVnodeBeforeUnmount: runCallbacks,
})
: h(
'div',
__VUEPRESS_DEV__
? 'Page does not exist. This is a fallback content.'
: '404 Not Found',
)
},
})
19 changes: 19 additions & 0 deletions plugins/plugin-content-update/src/client/composables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { onUnmounted } from 'vue'

// eslint-disable-next-line import/no-mutable-exports
export let contentUpdatedCallbacks: (() => any)[] = []

/**
* Register callback that is called every time the markdown content is updated
* in the DOM.
*/
export function onContentUpdated(fn: () => any) {
contentUpdatedCallbacks.push(fn)
onUnmounted(() => {
contentUpdatedCallbacks = contentUpdatedCallbacks.filter(f => f !== fn)
})
}

export function runCallbacks() {
contentUpdatedCallbacks.forEach(fn => fn())
}
2 changes: 2 additions & 0 deletions plugins/plugin-content-update/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './components/Content.js'
export { onContentUpdated } from './composables/index.js'
5 changes: 5 additions & 0 deletions plugins/plugin-content-update/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { contentUpdatePlugin } from './plugin.js'

export { contentUpdatePlugin }

export default contentUpdatePlugin
11 changes: 11 additions & 0 deletions plugins/plugin-content-update/src/node/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Plugin } from '@vuepress/core'
import { getDirname, path } from '@vuepress/utils'

const __dirname = getDirname(import.meta.url)

export function contentUpdatePlugin(): Plugin {
return {
name: '@vuepress-plume/plugin-content-update',
clientConfigFile: path.resolve(__dirname, '../client/clientConfig.js'),
}
}
4 changes: 4 additions & 0 deletions plugins/plugin-content-update/src/shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '@internal/pagesComponents' {
const pagesComponents: Record<string, any>
export { pagesComponents }
}
8 changes: 8 additions & 0 deletions plugins/plugin-content-update/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.build.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./lib"
},
"include": ["./src"]
}
3 changes: 2 additions & 1 deletion plugins/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
{ "path": "./plugin-netlify-functions/tsconfig.build.json" },
{ "path": "./plugin-notes-data/tsconfig.build.json" },
{ "path": "./plugin-page-collection/tsconfig.build.json" },
{ "path": "./plugin-shikiji/tsconfig.build.json" }
{ "path": "./plugin-shikiji/tsconfig.build.json" },
{ "path": "./plugin-content-update/tsconfig.build.json" }
],
"files": []
}
Loading

0 comments on commit 496bbae

Please sign in to comment.