mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
perf(plugin-md-power): 优化 pdf 支持
This commit is contained in:
parent
25d8aa1fb3
commit
8e9ed082c9
@ -1,7 +1,6 @@
|
||||
import { ensureEndingSlash } from 'vuepress/shared'
|
||||
import { ensureEndingSlash, isLinkHttp } from 'vuepress/shared'
|
||||
import { withBase } from 'vuepress/client'
|
||||
import type { PDFEmbedType, PDFTokenMeta } from '../../shared/pdf.js'
|
||||
import { normalizeLink } from '../utils/link.js'
|
||||
import { pluginOptions } from '../options.js'
|
||||
import { checkIsMobile, checkIsSafari, checkIsiPad } from '../utils/is.js'
|
||||
|
||||
@ -28,15 +27,18 @@ export function renderPDF(
|
||||
): void {
|
||||
if (!pluginOptions.pdf)
|
||||
return
|
||||
url = normalizeLink(url)
|
||||
url = isLinkHttp(url)
|
||||
? url
|
||||
: new URL(withBase(url), typeof location !== 'undefined' ? location.href : '').toString()
|
||||
|
||||
const pdfOptions = pluginOptions.pdf === true ? {} : pluginOptions.pdf
|
||||
const pdfjsUrl = pdfOptions.pdfjsUrl
|
||||
? `${ensureEndingSlash(withBase(pdfOptions.pdfjsUrl))}web/viewer.html`
|
||||
: ''
|
||||
pdfOptions.pdfjsUrl ??= 'https://static.pengzhanbo.cn/pdfjs/'
|
||||
const pdfjsUrl = `${ensureEndingSlash(withBase(pdfOptions.pdfjsUrl))}web/viewer.html`
|
||||
|
||||
const queryString = queryStringify(options)
|
||||
|
||||
const source = embedType === 'pdfjs'
|
||||
? `${pdfjsUrl}?file=${encodeURIComponent(url)}${queryString}`
|
||||
? `${pdfjsUrl}?file=${url}${queryString}`
|
||||
: `${url}${queryString}`
|
||||
|
||||
const tagName = embedType === 'pdfjs' || embedType === 'iframe'
|
||||
@ -100,8 +102,5 @@ export function usePDF(
|
||||
return renderPDF(el, url, embedType, options)
|
||||
}
|
||||
|
||||
if (typeof pluginOptions.pdf === 'object' && pluginOptions.pdf.pdfjsUrl)
|
||||
return renderPDF(el, url, 'pdfjs', options)
|
||||
|
||||
el.innerHTML = `<p>This browser does not support embedding PDFs. Please download the PDF to view it: <a href='${url}' target='_blank'>Download PDF</a></p>`
|
||||
return renderPDF(el, url, 'pdfjs', options)
|
||||
}
|
||||
|
||||
@ -10,13 +10,15 @@ import { parseRect } from '../../utils/parseRect.js'
|
||||
|
||||
type AddIcon = (iconName: string) => string | undefined
|
||||
|
||||
const [openTag, endTag] = [':[', ']:']
|
||||
|
||||
function createTokenizer(addIcon: AddIcon): RuleInline {
|
||||
return (state, silent) => {
|
||||
let found = false
|
||||
const max = state.posMax
|
||||
const start = state.pos
|
||||
|
||||
if (state.src.slice(start, start + 2) !== ':[')
|
||||
if (state.src.slice(start, start + 2) !== openTag)
|
||||
return false
|
||||
|
||||
if (silent)
|
||||
@ -29,7 +31,7 @@ function createTokenizer(addIcon: AddIcon): RuleInline {
|
||||
state.pos = start + 2
|
||||
|
||||
while (state.pos < max) {
|
||||
if (state.src.slice(state.pos, state.pos + 2) === ']:') {
|
||||
if (state.src.slice(state.pos, state.pos + 2) === endTag) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
@ -58,7 +60,7 @@ function createTokenizer(addIcon: AddIcon): RuleInline {
|
||||
const [size, color] = options.split('/')
|
||||
|
||||
const open = state.push('iconify_open', 'span', 1)
|
||||
open.markup = ':['
|
||||
open.markup = openTag
|
||||
|
||||
const className = addIcon(iconName)
|
||||
|
||||
@ -79,7 +81,7 @@ function createTokenizer(addIcon: AddIcon): RuleInline {
|
||||
text.content = className ? '' : iconName
|
||||
|
||||
const close = state.push('iconify_close', 'span', -1)
|
||||
close.markup = ']:'
|
||||
close.markup = endTag
|
||||
|
||||
state.pos = state.posMax + 2
|
||||
state.posMax = max
|
||||
|
||||
@ -51,7 +51,7 @@ function createPDFRuleBlock(): RuleBlock {
|
||||
src,
|
||||
page: +page || 1,
|
||||
noToolbar: Boolean(attrs.noToolbar ?? false),
|
||||
zoom: +attrs.zoom || 1,
|
||||
zoom: +attrs.zoom || 50,
|
||||
width: attrs.width ? parseRect(attrs.width) : '100%',
|
||||
height: attrs.height ? parseRect(attrs.height) : '',
|
||||
ratio: attrs.ratio ? parseRect(attrs.ratio) : '',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user