打开pdf中指定的页面

参照官网的插件例子。

import {App, MarkdownView, Notice} from 'obsidian'

  // registerViewWithExtensions

  // 处理clickevent
  export function click(evt: MouseEvent,app:App){
      const markdownView = app.workspace.getActiveViewOfType(MarkdownView);
      if (markdownView) {
          let mode = markdownView.getMode()
          let file = app.workspace.getActiveFile()
          if(mode == 'preview'){
             if(file){
                 let base = file.basename
                 if(base == 'contents'){
                    // when the file is contents
                    let src = evt.srcElement 
                    let node :Node|null = src as Node
                    if(node){
                      let text =  node.textContent
                      text = text? text.trim():null
                      let metacache = app.metadataCache.getFileCache(file)
                      let frontmatter = metacache?.frontmatter
                      let pdf = frontmatter && frontmatter['pdf']
                      let pre_page = frontmatter && frontmatter['pre_page'] || 1
                      if(pdf && text){
                          new Notice('in contents text :'+JSON.stringify(text) + '@'+pdf+':'+pre_page)
                          let match = text.match(/[0-9]+$/)
                          if(match){
                              let url = pdf+'#page='+(parseInt(pre_page)+parseInt(match[0]) )
                              app.workspace.openLinkText(url,pdf,false)
                          }
                      }
                    }
                 }
             }
          }else{
              // in 'source' mode
          }
          return true;
      }
  }
1 个赞