Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add markdown content hooks #1620

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script lang="ts" setup>
import { ref, watch } from 'vue'
import {
onContentBeforeUnmount,
onContentChange,
onContentMounted,
useRoutePath,
} from 'vuepress/client'

const mounted = ref('')
const beforeUnmount = ref('')

const mountedCount = ref(0)
const changedCount = ref(0)

const routePath = useRoutePath()

watch(routePath, () => {
changedCount.value = 0
})

onContentMounted(() => {
mounted.value = routePath.value
mountedCount.value++
})

onContentChange(() => {
changedCount.value++
})

onContentBeforeUnmount(() => {
beforeUnmount.value = mounted.value
})
</script>

<template>
<div class="markdown-content-hooks">
<p class="markdown-content-mounted">
mounted: {{ mounted }} {{ mountedCount }}
</p>
<p class="markdown-content-before-unmount">
beforeUnmount: {{ beforeUnmount }}
</p>
<p class="markdown-content-change">changedCount: {{ changedCount }}</p>
</div>
</template>
3 changes: 3 additions & 0 deletions e2e/docs/.vuepress/theme/client/layouts/Layout.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { Content, useSiteData } from 'vuepress/client'
import MarkdownContentHooks from '../components/MarkdownContentHooks.vue'

const siteData = useSiteData()
</script>
Expand All @@ -18,6 +19,8 @@ const siteData = useSiteData()
<main class="e2e-theme-content">
<Content />
</main>

<MarkdownContentHooks />
</div>
</template>

Expand Down
3 changes: 3 additions & 0 deletions e2e/docs/content-hooks/content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## title

content
64 changes: 64 additions & 0 deletions e2e/tests/content-hooks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect, test } from '@playwright/test'
import { IS_DEV } from '../utils/env'
import { readSourceMarkdown, writeSourceMarkdown } from '../utils/source'

const updateMarkdownContent = async (): Promise<void> => {
const content = await readSourceMarkdown('content-hooks/content.md')
await writeSourceMarkdown(
'content-hooks/content.md',
`${content}\n\nUpdated content`,
)
}

const restoreMarkdownContent = async (): Promise<void> => {
await writeSourceMarkdown('content-hooks/content.md', '## title\n\ncontent\n')
}

test.afterAll(async () => {
await restoreMarkdownContent()
})

test('should call content mounted hook', async ({ page }) => {
const mountedLocator = page.locator(
'.markdown-content-hooks .markdown-content-mounted',
)
await page.goto('content-hooks/content.html')

await expect(mountedLocator).toHaveText(
'mounted: /content-hooks/content.html 1',
)

// update content but mounted hook should not be called twice
await updateMarkdownContent()
await expect(mountedLocator).toHaveText(
'mounted: /content-hooks/content.html 1',
)
})

/**
* onContentChange hook should only called in development
*/
test('should call content change hook', async ({ page }) => {
const changeLocator = page.locator(
'.markdown-content-hooks .markdown-content-change',
)
await page.goto('content-hooks/content.html')

await updateMarkdownContent()
await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 18, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 18, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 18, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 20, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 7 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 20, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 20, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 22, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 7 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 22, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 22, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 18, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 7 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 18, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (ubuntu-latest, 18, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /home/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 20, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 8 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 20, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 20, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 22, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 7 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 22, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

Check failure on line 48 in e2e/tests/content-hooks.spec.ts

View workflow job for this annotation

GitHub Actions / e2e (macos-latest, 22, vite)

[chromium] › content-hooks.spec.ts:41:1 › should call content change hook

1) [chromium] › content-hooks.spec.ts:41:1 › should call content change hook ───────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('.markdown-content-hooks .markdown-content-change') Expected string: "changedCount: 1" Received string: "changedCount: 0" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('.markdown-content-hooks .markdown-content-change') 9 × locator resolved to <p class="markdown-content-change">changedCount: 0</p> - unexpected value "changedCount: 0" 46 | 47 | await updateMarkdownContent() > 48 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 1 : 0}`) // 1 | ^ 49 | 50 | await updateMarkdownContent() 51 | await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2 at /Users/runner/work/core/core/e2e/tests/content-hooks.spec.ts:48:31

await updateMarkdownContent()
await expect(changeLocator).toHaveText(`changedCount: ${IS_DEV ? 2 : 0}`) // 2
})

test('should call content before unmount hook', async ({ page }) => {
const beforeUnmountLocator = page.locator(
'.markdown-content-hooks .markdown-content-before-unmount',
)
await page.goto('content-hooks/content.html')
await page.locator('.e2e-theme-nav ul > li > a').nth(0).click()

await expect(beforeUnmountLocator).toHaveText(
'beforeUnmount: /content-hooks/content.html',
)
})
15 changes: 13 additions & 2 deletions packages/client/src/components/Content.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed, defineAsyncComponent, defineComponent, h } from 'vue'
import { usePageComponent } from '../composables/index.js'
import { runCallbacks, usePageComponent } from '../composables/index.js'
import { resolveRoute } from '../router/index.js'

/**
Expand All @@ -26,6 +26,17 @@ export const Content = defineComponent({
)
})

return () => h(ContentComponent.value)
return () =>
h(ContentComponent.value, {
onVnodeMounted: () => {
runCallbacks('mounted')
},
onVnodeUpdated: () => {
runCallbacks('change')
},
onVnodeBeforeUnmount: () => {
runCallbacks('beforeUnmount')
},
})
},
})
31 changes: 31 additions & 0 deletions packages/client/src/composables/contentHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { onUnmounted } from 'vue'

type LifeCycle = 'beforeUnmount' | 'change' | 'mounted'

const hooks: Record<LifeCycle, (() => unknown)[]> = {
mounted: [],
beforeUnmount: [],
change: [],
}

const createHook =
(lifeCycle: LifeCycle) =>
(fn: () => unknown): void => {
hooks[lifeCycle].push(fn)
onUnmounted(() => {
hooks[lifeCycle] = hooks[lifeCycle].filter((f) => f !== fn)
})
}

export const onContentChange = createHook('change')

export const onContentMounted = createHook('mounted')

export const onContentBeforeUnmount = createHook('beforeUnmount')

/**
* Call all registered callbacks
*/
export const runCallbacks = (lifeCycle: LifeCycle): void => {
hooks[lifeCycle].forEach((fn) => fn())
}
1 change: 1 addition & 0 deletions packages/client/src/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './clientData.js'
export * from './clientDataUtils.js'
export * from './updateHead.js'
export * from './contentHooks.js'
Loading