mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-05-01 12:38:12 +08:00
36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
/**
|
|
* @[pdf](/xxx)
|
|
* @[pdf 1](/xxx)
|
|
* @[pdf 1 no-toolbar width="100%" height="600px" zoom="1" ratio="1:1"](/xxx)
|
|
*/
|
|
import { path } from 'vuepress/utils'
|
|
import type { PluginWithOptions } from 'markdown-it'
|
|
import { createRuleBlock } from '../utils/createRuleBlock.js'
|
|
import { parseRect } from '../utils/parseRect.js'
|
|
import { resolveAttrs } from '../utils/resolveAttrs.js'
|
|
import type { PDFTokenMeta } from '../../shared/index.js'
|
|
|
|
export const pdfPlugin: PluginWithOptions<never> = (md) => {
|
|
createRuleBlock<PDFTokenMeta>(md, {
|
|
type: 'pdf',
|
|
// eslint-disable-next-line regexp/no-super-linear-backtracking
|
|
syntaxPattern: /^@\[pdf(?:\s+(\d+))?([^\]]*)\]\(([^)]*)\)/,
|
|
meta([, page, info = '', src = '']) {
|
|
const { attrs } = resolveAttrs(info)
|
|
return {
|
|
src,
|
|
page: +page || 1,
|
|
noToolbar: Boolean(attrs.noToolbar ?? false),
|
|
zoom: +attrs.zoom || 50,
|
|
width: attrs.width ? parseRect(attrs.width) : '100%',
|
|
height: attrs.height ? parseRect(attrs.height) : '',
|
|
ratio: attrs.ratio ? parseRect(attrs.ratio) : '',
|
|
title: path.basename(src || ''),
|
|
}
|
|
},
|
|
content({ title, src, page, noToolbar, width, height, ratio, zoom }) {
|
|
return `<PDFViewer src="${src}" title="${title}" :page="${page}" :no-toolbar="${noToolbar}" width="${width}" height="${height}" ratio="${ratio}" :zoom="${zoom}" />`
|
|
},
|
|
})
|
|
}
|