valaxy-theme-press 1.3.2 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  <script lang="ts" setup>
2
2
  import { isClient, useScrollLock } from '@vueuse/core'
3
- import { createMarkdownExit } from 'markdown-exit'
4
3
  import { useFuseSearch } from 'valaxy'
5
4
  import { nextTick, ref, watch } from 'vue'
6
5
  import { useI18n } from 'vue-i18n'
6
+ import { formatFuseExcerpt } from '../utils/excerpt'
7
7
 
8
8
  const props = defineProps<{
9
9
  open: boolean
@@ -11,11 +11,6 @@ const props = defineProps<{
11
11
 
12
12
  const emit = defineEmits(['close'])
13
13
 
14
- const md = createMarkdownExit({
15
- html: true,
16
- linkify: true,
17
- })
18
-
19
14
  const input = ref('')
20
15
  const isLocked = useScrollLock(isClient ? document.documentElement : null)
21
16
  const { t, locale } = useI18n()
@@ -113,7 +108,9 @@ function getResultKey(title: string | Record<string, string>): string {
113
108
  <h3 class="press-result-title">
114
109
  {{ getResultTitle(result.item.title) }}
115
110
  </h3>
116
- <p class="press-result-excerpt markdown-body" v-html="md.render(result.item.excerpt || '')" />
111
+ <p class="press-result-excerpt">
112
+ {{ formatFuseExcerpt(result.item.excerpt) }}
113
+ </p>
117
114
  <div class="press-result-meta">
118
115
  <span class="press-result-link">
119
116
  {{ result.item.link }}
@@ -1,7 +1,7 @@
1
1
  <script lang="ts" setup>
2
2
  import type { PageData, Post } from 'valaxy'
3
- import { onClickHref, onContentUpdated, scrollTo, useFrontmatter, useLayout, useSidebar, useSiteConfig, useValaxyI18n } from 'valaxy'
4
- import { computed, nextTick } from 'vue'
3
+ import { onClickHref, scrollTo, useFrontmatter, useLayout, useSidebar, useSiteConfig, useValaxyI18n } from 'valaxy'
4
+ import { computed, nextTick, onMounted, onUnmounted } from 'vue'
5
5
  import { useRoute, useRouter } from 'vue-router'
6
6
  import { targetPadding } from '../client'
7
7
 
@@ -34,8 +34,15 @@ nextTick(() => {
34
34
  }
35
35
  })
36
36
 
37
- onContentUpdated(() => {
38
- onClickHref(router)
37
+ let cleanupClickHref: (() => void) | undefined
38
+
39
+ onMounted(() => {
40
+ cleanupClickHref = onClickHref(router)
41
+ })
42
+
43
+ onUnmounted(() => {
44
+ cleanupClickHref?.()
45
+ cleanupClickHref = undefined
39
46
  })
40
47
  </script>
41
48
 
@@ -4,7 +4,10 @@ import type { PressTheme } from '../types'
4
4
  import { isClient } from '@vueuse/core'
5
5
  import {
6
6
  computed,
7
+ getCurrentInstance,
8
+ getCurrentScope,
7
9
  onMounted,
10
+ onScopeDispose,
8
11
  onUnmounted,
9
12
  ref,
10
13
  watch,
@@ -24,6 +27,23 @@ export interface SidebarControl {
24
27
 
25
28
  export const HASH_RE = /#.*$/
26
29
  export const EXT_RE = /(index)?\.(md|html)$/
30
+ const hashRef = ref('')
31
+ let hashListenerCount = 0
32
+
33
+ function onHashChange() {
34
+ hashRef.value = window.location.hash
35
+ }
36
+
37
+ /**
38
+ * Reset shared sidebar hash state cleanly between isolated unit tests.
39
+ */
40
+ export function resetSidebarHashState() {
41
+ if (isClient && hashListenerCount > 0)
42
+ window.removeEventListener('hashchange', onHashChange)
43
+ hashListenerCount = 0
44
+ hashRef.value = ''
45
+ }
46
+
27
47
  export function normalize(path: string): string {
28
48
  return decodeURI(path).replace(/[?#].*$/, '').replace(EXT_RE, '').replace(/\/$/, '') || '/'
29
49
  }
@@ -47,7 +67,7 @@ export function isActive(
47
67
  const hashMatch = matchPath.match(HASH_RE)
48
68
 
49
69
  if (hashMatch)
50
- return (isClient ? location.hash : '') === hashMatch[0]
70
+ return hashRef.value === hashMatch[0]
51
71
 
52
72
  return true
53
73
  }
@@ -84,11 +104,45 @@ export function useCloseSidebarOnEscape(
84
104
  }
85
105
  }
86
106
 
87
- const hashRef = ref(isClient ? location.hash : '')
88
- if (isClient) {
89
- window.addEventListener('hashchange', () => {
90
- hashRef.value = location.hash
107
+ function useClientHashTracker(route: ReturnType<typeof useRoute>, onSync: () => void) {
108
+ let registered = false
109
+
110
+ onMounted(() => {
111
+ if (!isClient)
112
+ return
113
+ if (!registered) {
114
+ if (hashListenerCount === 0)
115
+ window.addEventListener('hashchange', onHashChange)
116
+ hashListenerCount += 1
117
+ registered = true
118
+ }
119
+ hashRef.value = route.hash || window.location.hash
120
+ onSync()
91
121
  })
122
+
123
+ watch(
124
+ () => route.hash,
125
+ (newHash) => {
126
+ if (registered && newHash !== undefined)
127
+ hashRef.value = newHash
128
+ },
129
+ )
130
+
131
+ const cleanup = () => {
132
+ if (!registered)
133
+ return
134
+ registered = false
135
+ hashListenerCount = Math.max(0, hashListenerCount - 1)
136
+ if (isClient && hashListenerCount === 0) {
137
+ window.removeEventListener('hashchange', onHashChange)
138
+ hashRef.value = ''
139
+ }
140
+ }
141
+
142
+ if (getCurrentInstance())
143
+ onUnmounted(cleanup)
144
+ else if (getCurrentScope())
145
+ onScopeDispose(cleanup)
92
146
  }
93
147
 
94
148
  /**
@@ -128,7 +182,7 @@ export function useSidebarControl(
128
182
  }
129
183
 
130
184
  watch([() => route.path, item, hashRef], updateIsActiveLink, { immediate: true })
131
- onMounted(updateIsActiveLink)
185
+ useClientHashTracker(route, updateIsActiveLink)
132
186
 
133
187
  const hasActiveLink = computed(() => {
134
188
  if (isActiveLink.value)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valaxy-theme-press",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
4
4
  "description": "Docs (VitePress-like) theme for Valaxy",
5
5
  "author": {
6
6
  "email": "me@yunyoujun.cn",
@@ -37,6 +37,6 @@
37
37
  "reka-ui": "^2.10.4"
38
38
  },
39
39
  "devDependencies": {
40
- "valaxy": "1.3.2"
40
+ "valaxy": "1.3.4"
41
41
  }
42
42
  }
@@ -0,0 +1,149 @@
1
+ const NAMED_HTML_ENTITIES: Record<string, string> = {
2
+ amp: '&',
3
+ lt: '<',
4
+ gt: '>',
5
+ quot: '"',
6
+ apos: '\'',
7
+ nbsp: ' ',
8
+ copy: '©',
9
+ reg: '®',
10
+ trade: '™',
11
+ mdash: '—',
12
+ ndash: '–',
13
+ hellip: '…',
14
+ laquo: '«',
15
+ raquo: '»',
16
+ lsquo: '‘',
17
+ rsquo: '’',
18
+ ldquo: '“',
19
+ rdquo: '”',
20
+ }
21
+
22
+ let entityDecoderTextarea: HTMLTextAreaElement | undefined
23
+
24
+ function decodeNumericCodePoint(raw: string, radix: number): string | undefined {
25
+ const code = Number.parseInt(raw, radix)
26
+ if (!Number.isFinite(code) || code <= 0 || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF))
27
+ return undefined
28
+ return String.fromCodePoint(code)
29
+ }
30
+
31
+ function decodeHtmlEntities(text: string): string {
32
+ if (!text.includes('&'))
33
+ return text
34
+
35
+ return text.replace(/&(#x[\da-f]+|#\d+|[a-z][\da-z]+);/gi, (match, body: string) => {
36
+ if (body[0] === '#') {
37
+ const isHex = body[1] === 'x' || body[1] === 'X'
38
+ return decodeNumericCodePoint(body.slice(isHex ? 2 : 1), isHex ? 16 : 10) ?? match
39
+ }
40
+ const known = NAMED_HTML_ENTITIES[body]
41
+ if (known)
42
+ return known
43
+ if (typeof document !== 'undefined') {
44
+ entityDecoderTextarea ??= document.createElement('textarea')
45
+ entityDecoderTextarea.innerHTML = match
46
+ return entityDecoderTextarea.value
47
+ }
48
+ return match
49
+ })
50
+ }
51
+
52
+ const ESCAPABLE_PUNCTUATION_RE = /^[-!"#$%&'()*+,./:;<=>?@[\\\]^_`{|}~]$/
53
+
54
+ function findClosingBackticks(text: string, from: number, ticks: number): number {
55
+ let i = from
56
+ while (i < text.length) {
57
+ if (text[i] === '`') {
58
+ let run = 0
59
+ while (i + run < text.length && text[i + run] === '`')
60
+ run++
61
+ if (run === ticks)
62
+ return i
63
+ i += run
64
+ }
65
+ else {
66
+ i++
67
+ }
68
+ }
69
+ return -1
70
+ }
71
+
72
+ function protectCodeAndEscapes(text: string, protect: (value: string) => string): string {
73
+ let result = ''
74
+ let i = 0
75
+
76
+ while (i < text.length) {
77
+ const ch = text[i]
78
+ if (ch === '\\' && i + 1 < text.length && ESCAPABLE_PUNCTUATION_RE.test(text[i + 1])) {
79
+ result += protect(text[i + 1])
80
+ i += 2
81
+ continue
82
+ }
83
+
84
+ if (ch === '`') {
85
+ let ticks = 0
86
+ while (i + ticks < text.length && text[i + ticks] === '`')
87
+ ticks++
88
+
89
+ const closeIdx = findClosingBackticks(text, i + ticks, ticks)
90
+ if (closeIdx !== -1) {
91
+ const rawCode = text.slice(i + ticks, closeIdx)
92
+ const trimmedCode = rawCode.length >= 2
93
+ && rawCode.startsWith(' ')
94
+ && rawCode.endsWith(' ')
95
+ && /[^ ]/.test(rawCode)
96
+ ? rawCode.slice(1, -1)
97
+ : rawCode
98
+ result += protect(trimmedCode)
99
+ i = closeIdx + ticks
100
+ continue
101
+ }
102
+
103
+ result += text.slice(i, i + ticks)
104
+ i += ticks
105
+ continue
106
+ }
107
+
108
+ result += ch
109
+ i++
110
+ }
111
+
112
+ return result
113
+ }
114
+
115
+ /**
116
+ * Convert raw Markdown excerpt text to clean plain text for search results,
117
+ * preserving inline code spans, escaped punctuation, autolinks, and math/comparison prose.
118
+ */
119
+ export function formatFuseExcerpt(text = ''): string {
120
+ const literals: string[] = []
121
+ const protect = (value: string) => `\u0000${literals.push(value) - 1}\u0000`
122
+ let cleaned = protectCodeAndEscapes(
123
+ text
124
+ .replace(/\0/g, '')
125
+ .replace(/^[ \t]*```[\s\S]+?^[ \t]*```/gm, ' ')
126
+ .replace(/^[ \t]*~~~[\s\S]+?^[ \t]*~~~/gm, ' '),
127
+ protect,
128
+ )
129
+ .replace(/<!--[\s\S]*?(?:-->|$)/g, ' ')
130
+ .replace(/!\[[^\]]*\]\([^)]*\)/g, ' ')
131
+ .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
132
+ .replace(/<((?:https?:\/\/|mailto:)[^>\s]+)>/gi, '$1')
133
+ .replace(/<([^\s<>@]+@[^\s<>@.]+(?:\.[^\s<>@.]+)+)>/g, '$1')
134
+ .replace(/<\/?[a-z][\w:-]*(?:\s[^<>]*)?\/?>/gi, ' ')
135
+ .replace(/^[ \t]*#{1,6}[ \t]+/gm, '')
136
+ .replace(/^[ \t]*>[ \t]?/gm, '')
137
+
138
+ for (let depth = 0; depth < 3; depth++) {
139
+ cleaned = cleaned
140
+ .replace(/(?<![\p{L}\p{N}_])(_{1,3})(?=\S)(.+?\S|\S)\1(?![\p{L}\p{N}_])/gu, '$2')
141
+ .replace(/(?<!\*)(\*{1,3})(?=\S)(.+?\S|\S)\1(?!\*)/g, '$2')
142
+ .replace(/~~(?=\S)(.+?\S|\S)~~/g, '$1')
143
+ }
144
+
145
+ return decodeHtmlEntities(cleaned)
146
+ .replace(/\0(\d+)\0/g, (_, index: string) => literals[Number(index)])
147
+ .replace(/\s+/g, ' ')
148
+ .trim()
149
+ }