-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90465d2
commit 496bbae
Showing
14 changed files
with
254 additions
and
35 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
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. |
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,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() | ||
] | ||
// ... | ||
} | ||
``` |
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,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" | ||
] | ||
} |
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,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
42
plugins/plugin-content-update/src/client/components/Content.ts
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 { 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
19
plugins/plugin-content-update/src/client/composables/index.ts
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,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()) | ||
} |
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,2 @@ | ||
export * from './components/Content.js' | ||
export { onContentUpdated } from './composables/index.js' |
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,5 @@ | ||
import { contentUpdatePlugin } from './plugin.js' | ||
|
||
export { contentUpdatePlugin } | ||
|
||
export default contentUpdatePlugin |
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,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'), | ||
} | ||
} |
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,4 @@ | ||
declare module '@internal/pagesComponents' { | ||
const pagesComponents: Record<string, any> | ||
export { pagesComponents } | ||
} |
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,8 @@ | ||
{ | ||
"extends": "../tsconfig.build.json", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./lib" | ||
}, | ||
"include": ["./src"] | ||
} |
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
Oops, something went wrong.