vuepress-plugin-md-power 1.0.0-rc.80 → 1.0.0-rc.82

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.
Files changed (93) hide show
  1. package/lib/client/components/CodeEditor.vue +4 -3
  2. package/lib/client/components/CodeSandbox.vue +1 -1
  3. package/lib/client/components/PDFViewer.vue +1 -1
  4. package/lib/client/components/Plot.vue +1 -1
  5. package/lib/client/components/Replit.vue +1 -1
  6. package/lib/client/composables/codeRepl.d.ts +7 -5
  7. package/lib/client/composables/codeRepl.js +253 -145
  8. package/lib/client/composables/pdf.d.ts +7 -3
  9. package/lib/client/composables/pdf.js +55 -81
  10. package/lib/client/composables/rustRepl.d.ts +3 -2
  11. package/lib/client/composables/rustRepl.js +96 -94
  12. package/lib/client/composables/size.d.ts +9 -5
  13. package/lib/client/composables/size.js +36 -44
  14. package/lib/client/index.js +2 -1
  15. package/lib/client/options.d.ts +5 -2
  16. package/lib/client/options.js +5 -1
  17. package/lib/client/utils/http.d.ts +3 -1
  18. package/lib/client/utils/http.js +24 -20
  19. package/lib/client/utils/is.d.ts +5 -3
  20. package/lib/client/utils/is.js +16 -10
  21. package/lib/client/utils/link.d.ts +3 -1
  22. package/lib/client/utils/link.js +8 -4
  23. package/lib/client/utils/sleep.d.ts +3 -1
  24. package/lib/client/utils/sleep.js +8 -4
  25. package/lib/node/index.d.ts +188 -2
  26. package/lib/node/index.js +847 -2
  27. package/lib/shared/index.d.ts +185 -10
  28. package/lib/shared/index.js +0 -10
  29. package/package.json +9 -9
  30. package/lib/node/features/caniuse.d.ts +0 -25
  31. package/lib/node/features/caniuse.js +0 -87
  32. package/lib/node/features/codeSandbox.d.ts +0 -7
  33. package/lib/node/features/codeSandbox.js +0 -29
  34. package/lib/node/features/codepen.d.ts +0 -7
  35. package/lib/node/features/codepen.js +0 -42
  36. package/lib/node/features/icons/index.d.ts +0 -2
  37. package/lib/node/features/icons/index.js +0 -2
  38. package/lib/node/features/icons/plugin.d.ts +0 -10
  39. package/lib/node/features/icons/plugin.js +0 -61
  40. package/lib/node/features/icons/writer.d.ts +0 -11
  41. package/lib/node/features/icons/writer.js +0 -126
  42. package/lib/node/features/jsfiddle.d.ts +0 -6
  43. package/lib/node/features/jsfiddle.js +0 -28
  44. package/lib/node/features/langRepl.d.ts +0 -4
  45. package/lib/node/features/langRepl.js +0 -59
  46. package/lib/node/features/pdf.d.ts +0 -2
  47. package/lib/node/features/pdf.js +0 -31
  48. package/lib/node/features/plot.d.ts +0 -5
  49. package/lib/node/features/plot.js +0 -48
  50. package/lib/node/features/replit.d.ts +0 -7
  51. package/lib/node/features/replit.js +0 -22
  52. package/lib/node/features/video/bilibili.d.ts +0 -2
  53. package/lib/node/features/video/bilibili.js +0 -58
  54. package/lib/node/features/video/youtube.d.ts +0 -2
  55. package/lib/node/features/video/youtube.js +0 -47
  56. package/lib/node/plugin.d.ts +0 -3
  57. package/lib/node/plugin.js +0 -80
  58. package/lib/node/prepareConfigFile.d.ts +0 -3
  59. package/lib/node/prepareConfigFile.js +0 -59
  60. package/lib/node/utils/createRuleBlock.d.ts +0 -18
  61. package/lib/node/utils/createRuleBlock.js +0 -34
  62. package/lib/node/utils/package.d.ts +0 -4
  63. package/lib/node/utils/package.js +0 -4
  64. package/lib/node/utils/parseRect.d.ts +0 -1
  65. package/lib/node/utils/parseRect.js +0 -5
  66. package/lib/node/utils/resolveAttrs.d.ts +0 -4
  67. package/lib/node/utils/resolveAttrs.js +0 -29
  68. package/lib/node/utils/timeToSeconds.d.ts +0 -1
  69. package/lib/node/utils/timeToSeconds.js +0 -8
  70. package/lib/shared/caniuse.d.ts +0 -18
  71. package/lib/shared/caniuse.js +0 -1
  72. package/lib/shared/codeSandbox.d.ts +0 -11
  73. package/lib/shared/codeSandbox.js +0 -1
  74. package/lib/shared/codepen.d.ts +0 -10
  75. package/lib/shared/codepen.js +0 -1
  76. package/lib/shared/icons.d.ts +0 -17
  77. package/lib/shared/icons.js +0 -1
  78. package/lib/shared/jsfiddle.d.ts +0 -8
  79. package/lib/shared/jsfiddle.js +0 -1
  80. package/lib/shared/pdf.d.ts +0 -15
  81. package/lib/shared/pdf.js +0 -1
  82. package/lib/shared/plot.d.ts +0 -27
  83. package/lib/shared/plot.js +0 -1
  84. package/lib/shared/plugin.d.ts +0 -21
  85. package/lib/shared/plugin.js +0 -1
  86. package/lib/shared/repl.d.ts +0 -22
  87. package/lib/shared/repl.js +0 -1
  88. package/lib/shared/replit.d.ts +0 -6
  89. package/lib/shared/replit.js +0 -1
  90. package/lib/shared/size.d.ts +0 -5
  91. package/lib/shared/size.js +0 -1
  92. package/lib/shared/video.d.ts +0 -22
  93. package/lib/shared/video.js +0 -1
@@ -106,12 +106,13 @@ onUnmounted(() => {
106
106
  z-index: 1;
107
107
  box-sizing: border-box;
108
108
  display: block;
109
- padding: 1.3rem 1.5rem;
109
+ padding: 20px 24px;
110
110
  overflow-x: auto;
111
111
  font-family: var(--vp-font-family-mono);
112
112
  font-size: var(--vp-code-font-size);
113
113
  -webkit-hyphens: none;
114
114
  hyphens: none;
115
+ line-height: var(--vp-code-line-height);
115
116
  color: transparent;
116
117
  text-align: left;
117
118
  word-break: normal;
@@ -139,7 +140,7 @@ onUnmounted(() => {
139
140
  }
140
141
 
141
142
  :deep(div[class*="language-"].line-numbers-mode) + .code-repl-input {
142
- padding-left: 1rem;
143
- margin-left: 2rem;
143
+ padding-left: 24px;
144
+ margin-left: 32px;
144
145
  }
145
146
  </style>
@@ -1,6 +1,6 @@
1
1
  <script setup lang="ts">
2
2
  import { computed } from 'vue'
3
- import type { CodeSandboxTokenMeta } from '../../shared/codeSandbox.js'
3
+ import type { CodeSandboxTokenMeta } from '../../shared/index.js'
4
4
 
5
5
  const props = defineProps<CodeSandboxTokenMeta>()
6
6
 
@@ -1,6 +1,6 @@
1
1
  <script setup lang="ts">
2
2
  import { onMounted, toRefs } from 'vue'
3
- import type { PDFTokenMeta } from '../../shared/pdf.js'
3
+ import type { PDFTokenMeta } from '../../shared/index.js'
4
4
  import { useSize } from '../composables/size.js'
5
5
  import { usePDF } from '../composables/pdf.js'
6
6
 
@@ -2,7 +2,7 @@
2
2
  import { computed, ref, shallowRef } from 'vue'
3
3
  import { onClickOutside, useMediaQuery } from '@vueuse/core'
4
4
  import { usePageFrontmatter } from 'vuepress/client'
5
- import type { PlotOptions } from '../../shared/plot.js'
5
+ import type { PlotOptions } from '../../shared/index.js'
6
6
  import { pluginOptions } from '../options.js'
7
7
 
8
8
  const props = defineProps<Omit<PlotOptions, 'tag'>>()
@@ -1,6 +1,6 @@
1
1
  <script setup lang="ts">
2
2
  import { computed, getCurrentInstance, ref } from 'vue'
3
- import type { ReplitTokenMeta } from '../../shared/replit.js'
3
+ import type { ReplitTokenMeta } from '../../shared/index.js'
4
4
  import Loading from './Loading.vue'
5
5
 
6
6
  const props = defineProps<ReplitTokenMeta>()
@@ -1,11 +1,12 @@
1
- import { type Ref } from 'vue';
1
+ import { Ref } from 'vue';
2
+
2
3
  type Lang = 'kotlin' | 'go' | 'rust';
3
- export declare function resolveCode(el: HTMLElement): string;
4
- export declare function resolveCodeInfo(el: HTMLDivElement): {
4
+ declare function resolveCode(el: HTMLElement): string;
5
+ declare function resolveCodeInfo(el: HTMLDivElement): {
5
6
  lang: Lang;
6
7
  code: string;
7
8
  };
8
- export declare function useCodeRepl(el: Ref<HTMLDivElement | null>): {
9
+ declare function useCodeRepl(el: Ref<HTMLDivElement | null>): {
9
10
  onRunCode: () => Promise<void>;
10
11
  onCleanRun: () => void;
11
12
  lang: Ref<Lang | undefined>;
@@ -17,4 +18,5 @@ export declare function useCodeRepl(el: Ref<HTMLDivElement | null>): {
17
18
  finished: Ref<boolean>;
18
19
  error: Ref<string>;
19
20
  };
20
- export {};
21
+
22
+ export { resolveCode, resolveCodeInfo, useCodeRepl };
@@ -1,158 +1,266 @@
1
- import { onMounted, ref } from 'vue';
2
- import { http } from '../utils/http.js';
3
- import { sleep } from '../utils/sleep.js';
4
- import { rustExecute } from './rustRepl.js';
5
- const ignoredNodes = ['.diff.remove', '.vp-copy-ignore'];
6
- const RE_LANGUAGE = /language-(\w+)/;
7
- const api = {
8
- go: 'https://api.pengzhanbo.cn/repl/golang/run',
9
- kotlin: 'https://api.pengzhanbo.cn/repl/kotlin/run',
1
+ // src/client/composables/codeRepl.ts
2
+ import { onMounted, ref } from "vue";
3
+ import { http } from "../utils/http.js";
4
+ import { sleep } from "../utils/sleep.js";
5
+
6
+ // src/client/composables/rustRepl.ts
7
+ import { tryOnScopeDispose } from "@vueuse/core";
8
+ var wsUrl = "wss://play.rust-lang.org/websocket";
9
+ var payloadType = {
10
+ connected: "websocket/connected",
11
+ request: "output/execute/wsExecuteRequest",
12
+ execute: {
13
+ begin: "output/execute/wsExecuteBegin",
14
+ // status: 'output/execute/wsExecuteStatus',
15
+ stderr: "output/execute/wsExecuteStderr",
16
+ stdout: "output/execute/wsExecuteStdout",
17
+ end: "output/execute/wsExecuteEnd"
18
+ }
10
19
  };
11
- const langAlias = {
12
- kt: 'kotlin',
13
- kotlin: 'kotlin',
14
- go: 'go',
15
- rust: 'rust',
16
- rs: 'rust',
20
+ var ws = null;
21
+ var isOpen = false;
22
+ var uuid = 0;
23
+ function connect() {
24
+ if (isOpen)
25
+ return Promise.resolve();
26
+ ws = new WebSocket(wsUrl);
27
+ uuid = 0;
28
+ ws.addEventListener("open", () => {
29
+ isOpen = true;
30
+ send(
31
+ payloadType.connected,
32
+ { iAcceptThisIsAnUnsupportedApi: true },
33
+ { websocket: true, sequenceNumber: uuid }
34
+ );
35
+ });
36
+ ws.addEventListener("close", () => {
37
+ isOpen = false;
38
+ ws = null;
39
+ });
40
+ tryOnScopeDispose(() => ws?.close());
41
+ return new Promise((resolve) => {
42
+ function connected(e) {
43
+ const data = JSON.parse(e.data);
44
+ if (data.type === payloadType.connected) {
45
+ ws?.removeEventListener("message", connected);
46
+ resolve();
47
+ }
48
+ }
49
+ ws?.addEventListener("message", connected);
50
+ });
51
+ }
52
+ function send(type, payload, meta) {
53
+ const msg = { type, meta, payload };
54
+ ws?.send(JSON.stringify(msg));
55
+ }
56
+ async function rustExecute(code, { onEnd, onError, onStderr, onStdout, onBegin }) {
57
+ await connect();
58
+ const meta = { sequenceNumber: uuid++ };
59
+ const payload = {
60
+ backtrace: false,
61
+ channel: "stable",
62
+ crateType: "bin",
63
+ edition: "2021",
64
+ mode: "release",
65
+ tests: false,
66
+ code
67
+ };
68
+ send(payloadType.request, payload, meta);
69
+ let stdout = "";
70
+ let stderr = "";
71
+ function onMessage(e) {
72
+ const data = JSON.parse(e.data);
73
+ const { type, payload: payload2, meta: _meta = {} } = data;
74
+ if (_meta.sequenceNumber !== meta.sequenceNumber)
75
+ return;
76
+ if (type === payloadType.execute.begin)
77
+ onBegin?.();
78
+ if (type === payloadType.execute.stdout) {
79
+ stdout += payload2;
80
+ if (stdout.endsWith("\n")) {
81
+ onStdout?.(stdout);
82
+ stdout = "";
83
+ }
84
+ }
85
+ if (type === payloadType.execute.stderr) {
86
+ stderr += payload2;
87
+ if (stderr.endsWith("\n")) {
88
+ if (stderr.startsWith("error:")) {
89
+ const index = stderr.indexOf("\n");
90
+ onStderr?.(stderr.slice(0, index));
91
+ onStderr?.(stderr.slice(index + 1));
92
+ } else {
93
+ onStderr?.(stderr);
94
+ }
95
+ stderr = "";
96
+ }
97
+ }
98
+ if (type === payloadType.execute.end) {
99
+ if (payload2.success === false)
100
+ onError?.(payload2.exitDetail);
101
+ ws?.removeEventListener("message", onMessage);
102
+ onEnd?.();
103
+ }
104
+ }
105
+ ws?.addEventListener("message", onMessage);
106
+ }
107
+
108
+ // src/client/composables/codeRepl.ts
109
+ var ignoredNodes = [".diff.remove", ".vp-copy-ignore"];
110
+ var RE_LANGUAGE = /language-(\w+)/;
111
+ var api = {
112
+ go: "https://api.pengzhanbo.cn/repl/golang/run",
113
+ kotlin: "https://api.pengzhanbo.cn/repl/kotlin/run"
17
114
  };
18
- const supportLang = ['kotlin', 'go', 'rust'];
115
+ var langAlias = {
116
+ kt: "kotlin",
117
+ kotlin: "kotlin",
118
+ go: "go",
119
+ rust: "rust",
120
+ rs: "rust"
121
+ };
122
+ var supportLang = ["kotlin", "go", "rust"];
19
123
  function resolveLang(lang) {
20
- return lang ? langAlias[lang] || lang : '';
124
+ return lang ? langAlias[lang] || lang : "";
21
125
  }
22
- export function resolveCode(el) {
23
- const clone = el.cloneNode(true);
24
- clone
25
- .querySelectorAll(ignoredNodes.join(','))
26
- .forEach(node => node.remove());
27
- return clone.textContent || '';
126
+ function resolveCode(el) {
127
+ const clone = el.cloneNode(true);
128
+ clone.querySelectorAll(ignoredNodes.join(",")).forEach((node) => node.remove());
129
+ return clone.textContent || "";
28
130
  }
29
- export function resolveCodeInfo(el) {
30
- const wrapper = el.querySelector('div[class*=language-]');
31
- const lang = wrapper?.className.match(RE_LANGUAGE)?.[1];
32
- const codeEl = wrapper?.querySelector('pre');
33
- let code = '';
34
- if (codeEl)
35
- code = resolveCode(codeEl);
36
- return { lang: resolveLang(lang), code };
131
+ function resolveCodeInfo(el) {
132
+ const wrapper = el.querySelector("div[class*=language-]");
133
+ const lang = wrapper?.className.match(RE_LANGUAGE)?.[1];
134
+ const codeEl = wrapper?.querySelector("pre");
135
+ let code = "";
136
+ if (codeEl)
137
+ code = resolveCode(codeEl);
138
+ return { lang: resolveLang(lang), code };
37
139
  }
38
- export function useCodeRepl(el) {
39
- const lang = ref();
40
- const loaded = ref(true);
41
- const firstRun = ref(true);
42
- const finished = ref(true);
43
- const stdout = ref([]); // like print
44
- const stderr = ref([]); // like print error
45
- const error = ref(''); // execute error
46
- const backendVersion = ref('');
47
- onMounted(() => {
48
- if (el.value) {
49
- const info = resolveCodeInfo(el.value);
50
- lang.value = info.lang;
51
- }
140
+ function useCodeRepl(el) {
141
+ const lang = ref();
142
+ const loaded = ref(true);
143
+ const firstRun = ref(true);
144
+ const finished = ref(true);
145
+ const stdout = ref([]);
146
+ const stderr = ref([]);
147
+ const error = ref("");
148
+ const backendVersion = ref("");
149
+ onMounted(() => {
150
+ if (el.value) {
151
+ const info = resolveCodeInfo(el.value);
152
+ lang.value = info.lang;
153
+ }
154
+ });
155
+ const executeMap = {
156
+ kotlin: executeKotlin,
157
+ go: executeGolang,
158
+ rust: executeRust
159
+ };
160
+ function onCleanRun() {
161
+ loaded.value = false;
162
+ finished.value = false;
163
+ stdout.value = [];
164
+ stderr.value = [];
165
+ error.value = "";
166
+ firstRun.value = true;
167
+ backendVersion.value = "";
168
+ }
169
+ async function onRunCode() {
170
+ if (!el.value || !loaded.value)
171
+ return;
172
+ const info = resolveCodeInfo(el.value);
173
+ lang.value = info.lang;
174
+ if (!lang.value || !info.code || !supportLang.includes(lang.value))
175
+ return;
176
+ if (firstRun.value)
177
+ firstRun.value = false;
178
+ loaded.value = false;
179
+ finished.value = false;
180
+ stdout.value = [];
181
+ stderr.value = [];
182
+ error.value = "";
183
+ await executeMap[lang.value]?.(info.code);
184
+ }
185
+ async function executeGolang(code) {
186
+ const res = await http.post(api.go, { code });
187
+ backendVersion.value = `v${res.version}`;
188
+ loaded.value = true;
189
+ if (res.error) {
190
+ error.value = res.error;
191
+ finished.value = true;
192
+ return;
193
+ }
194
+ const events = res.events || [];
195
+ for (const event of events) {
196
+ if (event.kind === "stdout") {
197
+ if (event.delay)
198
+ await sleep(event.delay / 1e6);
199
+ stdout.value.push(event.message);
200
+ } else if (event.kind === "stderr") {
201
+ stderr.value.push(event.message);
202
+ }
203
+ }
204
+ finished.value = true;
205
+ }
206
+ async function executeKotlin(code) {
207
+ const filename = "File.kt";
208
+ const res = await http.post(api.kotlin, {
209
+ args: "",
210
+ files: [{ name: filename, publicId: "", text: code }]
52
211
  });
53
- const executeMap = {
54
- kotlin: executeKotlin,
55
- go: executeGolang,
56
- rust: executeRust,
57
- };
58
- function onCleanRun() {
59
- loaded.value = false;
60
- finished.value = false;
61
- stdout.value = [];
62
- stderr.value = [];
63
- error.value = '';
64
- firstRun.value = true;
65
- backendVersion.value = '';
212
+ backendVersion.value = `v${res.version}`;
213
+ loaded.value = true;
214
+ if (res.errors) {
215
+ const errors = Array.isArray(res.errors[filename]) ? res.errors[filename] : [res.errors[filename]];
216
+ if (errors.length) {
217
+ errors.forEach(
218
+ ({ message, severity }) => severity === "ERROR" && stderr.value.push(message)
219
+ );
220
+ }
66
221
  }
67
- async function onRunCode() {
68
- if (!el.value || !loaded.value)
69
- return;
70
- const info = resolveCodeInfo(el.value);
71
- lang.value = info.lang;
72
- if (!lang.value || !info.code || !supportLang.includes(lang.value))
73
- return;
74
- if (firstRun.value)
75
- firstRun.value = false;
76
- loaded.value = false;
222
+ stdout.value.push(res.text);
223
+ finished.value = true;
224
+ }
225
+ async function executeRust(code) {
226
+ await rustExecute(code, {
227
+ onBegin: () => {
228
+ loaded.value = true;
77
229
  finished.value = false;
78
230
  stdout.value = [];
79
231
  stderr.value = [];
80
- error.value = '';
81
- await executeMap[lang.value]?.(info.code);
82
- }
83
- async function executeGolang(code) {
84
- const res = await http.post(api.go, { code });
85
- backendVersion.value = `v${res.version}`;
86
- loaded.value = true;
87
- if (res.error) {
88
- error.value = res.error;
89
- finished.value = true;
90
- return;
91
- }
92
- const events = res.events || [];
93
- for (const event of events) {
94
- if (event.kind === 'stdout') {
95
- if (event.delay)
96
- await sleep(event.delay / 1000000);
97
- stdout.value.push(event.message);
98
- }
99
- else if (event.kind === 'stderr') {
100
- stderr.value.push(event.message);
101
- }
102
- }
103
- finished.value = true;
104
- }
105
- async function executeKotlin(code) {
106
- const filename = 'File.kt';
107
- const res = await http.post(api.kotlin, {
108
- args: '',
109
- files: [{ name: filename, publicId: '', text: code }],
110
- });
111
- backendVersion.value = `v${res.version}`;
112
- loaded.value = true;
113
- if (res.errors) {
114
- const errors = Array.isArray(res.errors[filename]) ? res.errors[filename] : [res.errors[filename]];
115
- if (errors.length) {
116
- errors.forEach(({ message, severity }) => severity === 'ERROR' && stderr.value.push(message));
117
- }
118
- }
119
- stdout.value.push(res.text);
232
+ error.value = "";
233
+ backendVersion.value = "release";
234
+ },
235
+ onError(message) {
236
+ error.value = message;
237
+ },
238
+ onStdout(message) {
239
+ stdout.value.push(message);
240
+ },
241
+ onStderr(message) {
242
+ stderr.value.push(message);
243
+ },
244
+ onEnd: () => {
120
245
  finished.value = true;
121
- }
122
- async function executeRust(code) {
123
- await rustExecute(code, {
124
- onBegin: () => {
125
- loaded.value = true;
126
- finished.value = false;
127
- stdout.value = [];
128
- stderr.value = [];
129
- error.value = '';
130
- backendVersion.value = 'release';
131
- },
132
- onError(message) {
133
- error.value = message;
134
- },
135
- onStdout(message) {
136
- stdout.value.push(message);
137
- },
138
- onStderr(message) {
139
- stderr.value.push(message);
140
- },
141
- onEnd: () => {
142
- finished.value = true;
143
- },
144
- });
145
- }
146
- return {
147
- onRunCode,
148
- onCleanRun,
149
- lang,
150
- backendVersion,
151
- firstRun,
152
- stderr,
153
- stdout,
154
- loaded,
155
- finished,
156
- error,
157
- };
246
+ }
247
+ });
248
+ }
249
+ return {
250
+ onRunCode,
251
+ onCleanRun,
252
+ lang,
253
+ backendVersion,
254
+ firstRun,
255
+ stderr,
256
+ stdout,
257
+ loaded,
258
+ finished,
259
+ error
260
+ };
158
261
  }
262
+ export {
263
+ resolveCode,
264
+ resolveCodeInfo,
265
+ useCodeRepl
266
+ };
@@ -1,3 +1,5 @@
1
+ import { PDFEmbedType, PDFTokenMeta } from '../../shared/index.js';
2
+
1
3
  /**
2
4
  * Fork for https://github.com/vuepress-theme-hope/vuepress-theme-hope/blob/main/packages/components/src/client/utils/viewPDF.ts
3
5
  *
@@ -10,6 +12,8 @@
10
12
  *
11
13
  * 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.
12
14
  */
13
- import type { PDFEmbedType, PDFTokenMeta } from '../../shared/pdf.js';
14
- export declare function renderPDF(el: HTMLElement, url: string, embedType: PDFEmbedType, options: PDFTokenMeta): void;
15
- export declare function usePDF(el: HTMLElement, url: string, options: PDFTokenMeta): void;
15
+
16
+ declare function renderPDF(el: HTMLElement, url: string, embedType: PDFEmbedType, options: PDFTokenMeta): void;
17
+ declare function usePDF(el: HTMLElement, url: string, options: PDFTokenMeta): void;
18
+
19
+ export { renderPDF, usePDF };
@@ -1,85 +1,59 @@
1
- /**
2
- * Fork for https://github.com/vuepress-theme-hope/vuepress-theme-hope/blob/main/packages/components/src/client/utils/viewPDF.ts
3
- *
4
- * The MIT License (MIT)
5
- * Copyright (C) 2021 - PRESENT by Mr.Hope<mister-hope@outlook.com>
6
- *
7
- * 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:
8
- *
9
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
- *
11
- * 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.
12
- */
13
- import { ensureEndingSlash, isLinkHttp } from 'vuepress/shared';
14
- import { withBase } from 'vuepress/client';
15
- import { pluginOptions } from '../options.js';
16
- import { checkIsMobile, checkIsSafari, checkIsiPad } from '../utils/is.js';
1
+ // src/client/composables/pdf.ts
2
+ import { ensureEndingSlash, isLinkHttp } from "vuepress/shared";
3
+ import { withBase } from "vuepress/client";
4
+ import { pluginOptions } from "../options.js";
5
+ import { checkIsMobile, checkIsSafari, checkIsiPad } from "../utils/is.js";
17
6
  function queryStringify(options) {
18
- const { page, noToolbar, zoom } = options;
19
- const params = [
20
- `page=${page}`,
21
- `toolbar=${noToolbar ? 0 : 1}`,
22
- `zoom=${zoom}`,
23
- ];
24
- let queryString = params.join('&');
25
- if (queryString)
26
- queryString = `#${queryString}`;
27
- return queryString;
7
+ const { page, noToolbar, zoom } = options;
8
+ const params = [
9
+ `page=${page}`,
10
+ `toolbar=${noToolbar ? 0 : 1}`,
11
+ `zoom=${zoom}`
12
+ ];
13
+ let queryString = params.join("&");
14
+ if (queryString)
15
+ queryString = `#${queryString}`;
16
+ return queryString;
28
17
  }
29
- export function renderPDF(el, url, embedType, options) {
30
- if (!pluginOptions.pdf)
31
- return;
32
- url = isLinkHttp(url)
33
- ? url
34
- : new URL(withBase(url), typeof location !== 'undefined' ? location.href : '').toString();
35
- const pdfOptions = pluginOptions.pdf === true ? {} : pluginOptions.pdf;
36
- pdfOptions.pdfjsUrl ??= 'https://static.pengzhanbo.cn/pdfjs/';
37
- const pdfjsUrl = `${ensureEndingSlash(withBase(pdfOptions.pdfjsUrl))}web/viewer.html`;
38
- const queryString = queryStringify(options);
39
- const source = embedType === 'pdfjs'
40
- ? `${pdfjsUrl}?file=${url}${queryString}`
41
- : `${url}${queryString}`;
42
- const tagName = embedType === 'pdfjs' || embedType === 'iframe'
43
- ? 'iframe'
44
- : 'embed';
45
- el.innerHTML = '';
46
- const pdf = document.createElement(tagName);
47
- pdf.className = 'pdf-viewer';
48
- pdf.type = 'application/pdf';
49
- pdf.title = options.title || 'PDF Viewer';
50
- pdf.src = source;
51
- if (pdf instanceof HTMLIFrameElement)
52
- pdf.allow = 'fullscreen';
53
- el.appendChild(pdf);
18
+ function renderPDF(el, url, embedType, options) {
19
+ if (!pluginOptions.pdf)
20
+ return;
21
+ url = isLinkHttp(url) ? url : new URL(withBase(url), typeof location !== "undefined" ? location.href : "").toString();
22
+ const pdfOptions = pluginOptions.pdf === true ? {} : pluginOptions.pdf;
23
+ pdfOptions.pdfjsUrl ??= "https://static.pengzhanbo.cn/pdfjs/";
24
+ const pdfjsUrl = `${ensureEndingSlash(withBase(pdfOptions.pdfjsUrl))}web/viewer.html`;
25
+ const queryString = queryStringify(options);
26
+ const source = embedType === "pdfjs" ? `${pdfjsUrl}?file=${url}${queryString}` : `${url}${queryString}`;
27
+ const tagName = embedType === "pdfjs" || embedType === "iframe" ? "iframe" : "embed";
28
+ el.innerHTML = "";
29
+ const pdf = document.createElement(tagName);
30
+ pdf.className = "pdf-viewer";
31
+ pdf.type = "application/pdf";
32
+ pdf.title = options.title || "PDF Viewer";
33
+ pdf.src = source;
34
+ if (pdf instanceof HTMLIFrameElement)
35
+ pdf.allow = "fullscreen";
36
+ el.appendChild(pdf);
54
37
  }
55
- export function usePDF(el, url, options) {
56
- if (typeof window === 'undefined' || !window?.navigator?.userAgent)
57
- return;
58
- const { navigator } = window;
59
- const { userAgent } = navigator;
60
- const isModernBrowser = typeof window.Promise === 'function';
61
- // Quick test for mobile devices.
62
- const isMobileDevice = checkIsiPad(userAgent) || checkIsMobile(userAgent);
63
- // Safari desktop requires special handling
64
- const isSafariDesktop = !isMobileDevice && checkIsSafari(userAgent);
65
- const isFirefoxWithPDFJS = !isMobileDevice
66
- && /firefox/iu.test(userAgent)
67
- && userAgent.split('rv:').length > 1
68
- ? Number.parseInt(userAgent.split('rv:')[1].split('.')[0], 10) > 18
69
- : false;
70
- // Determines whether PDF support is available
71
- const supportsPDFs
72
- // As of Sept 2020 no mobile browsers properly support PDF embeds
73
- = !isMobileDevice
74
- // We're moving into the age of MIME-less browsers. They mostly all support PDF rendering without plugins.
75
- && (isModernBrowser
76
- // Modern versions of Firefox come bundled with PDFJS
77
- || isFirefoxWithPDFJS);
78
- if (!url)
79
- return;
80
- if (supportsPDFs || !isMobileDevice) {
81
- const embedType = isSafariDesktop ? 'iframe' : 'embed';
82
- return renderPDF(el, url, embedType, options);
83
- }
84
- return renderPDF(el, url, 'pdfjs', options);
38
+ function usePDF(el, url, options) {
39
+ if (typeof window === "undefined" || !window?.navigator?.userAgent)
40
+ return;
41
+ const { navigator } = window;
42
+ const { userAgent } = navigator;
43
+ const isModernBrowser = typeof window.Promise === "function";
44
+ const isMobileDevice = checkIsiPad(userAgent) || checkIsMobile(userAgent);
45
+ const isSafariDesktop = !isMobileDevice && checkIsSafari(userAgent);
46
+ const isFirefoxWithPDFJS = !isMobileDevice && /firefox/iu.test(userAgent) && userAgent.split("rv:").length > 1 ? Number.parseInt(userAgent.split("rv:")[1].split(".")[0], 10) > 18 : false;
47
+ const supportsPDFs = !isMobileDevice && (isModernBrowser || isFirefoxWithPDFJS);
48
+ if (!url)
49
+ return;
50
+ if (supportsPDFs || !isMobileDevice) {
51
+ const embedType = isSafariDesktop ? "iframe" : "embed";
52
+ return renderPDF(el, url, embedType, options);
53
+ }
54
+ return renderPDF(el, url, "pdfjs", options);
85
55
  }
56
+ export {
57
+ renderPDF,
58
+ usePDF
59
+ };