Skip to content

Commit

Permalink
Transfer plugin to github template repo
Browse files Browse the repository at this point in the history
  • Loading branch information
JayGee0 committed Apr 2, 2024
0 parents commit eef0787
Show file tree
Hide file tree
Showing 16 changed files with 2,646 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 4
tab_width = 4
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/

main.js
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": { "node": true },
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
}
}
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Release Obsidian Plugin

on:
push:
tags:
- "*"

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"

- name: Build plugin
run: |
npm install
npm run build
- name: Create release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tag="${GITHUB_REF#refs/tags/}"
gh release create "$tag" \
--title="$tag" \
--draft \
main.js manifest.json styles.css
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# vscode
.vscode

# Intellij
*.iml
.idea

# npm
node_modules

# Don't include the compiled main.js file in the repo.
# They should be uploaded to GitHub releases instead.
main.js

# Exclude sourcemaps
*.map

# obsidian
data.json

# Exclude macOS Finder (System Explorer) View States
.DS_Store
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tag-version-prefix=""
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Obsidian PDF to Images

This plugin takes a pdf which is in the vault and converts them all to images,

**Note:** The Obsidian API is still in early alpha and is subject to change at any time!

**Dependencies:**
https://github.com/yakovmeister/pdf2image/blob/master/docs/gm-installation.md

## Usage
Click on the icon to the left and select a pdf file from the vault.
48 changes: 48 additions & 0 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";

const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;

const prod = (process.argv[2] === "production");

const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
bundle: true,
external: [
"obsidian",
"electron",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...builtins],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
});

if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}
93 changes: 93 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { App, Editor, MarkdownView, Modal, Notice, Menu, Plugin, PluginSettingTab, SuggestModal, Setting, TFile, Vault } from 'obsidian';
import { fromPath } from "pdf2pic";

// Remember to rename these classes and interfaces!

interface ConvertPluginSettings {
mySetting: string;
}

const DEFAULT_SETTINGS: ConvertPluginSettings = {
mySetting: 'default'
}

export default class ConvertPlugin extends Plugin {
settings: ConvertPluginSettings;

async onload() {
await this.loadSettings();

this.addRibbonIcon('file-down', 'Import PDF as image', (event) => {
new FileModal(this.app).open();
});

// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled.
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
});

// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
}

onunload() {

}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
}


export class FileModal extends SuggestModal<TFile> {
getSuggestions(query: string): TFile[] | Promise<TFile[]> {
return this.app.vault.getFiles().filter(t => {
return t.extension.toLowerCase() == "pdf" && t.name.toLowerCase().includes(query.toLowerCase());
})
}

renderSuggestion(value: TFile, el: HTMLElement) {
el.createEl("div", {text: value.name});
el.createEl("small", {text: value.path});
}
onChooseSuggestion(item: TFile, evt: MouseEvent | KeyboardEvent) {
const baseOptions = {
density: 100,
preserveAspectRatio: true,
format: "jpeg",
saveFilename: item.basename,
savePath: "",
}
const uri = this.app.vault.getResourcePath(item);
const split = uri.split('/');
var filepath = '';
var savepath = '';
for(let i = 3; i < split.length - 1; i++) {
filepath += split[i] + "/";
}

savepath = decodeURI(filepath);
filepath = decodeURI(filepath + item.name);

baseOptions.savePath = savepath;
const convert = fromPath(filepath, baseOptions).bulk(-1);
convert.then((imagesList) => {
const currentDoc = this.app.workspace.getActiveViewOfType(MarkdownView);
if(currentDoc) {
this.app.vault.process(currentDoc.file!, data => {
imagesList.forEach((f) => {
data += `![[${f.name}|500]]\n`;
})
return data;
})

}
})
}

}
10 changes: 10 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "obsidian-pdf-to-images",
"name": "Import PDF as images",
"version": "1.0.0",
"minAppVersion": "0.15.0",
"description": "Gets each page of the pdf and imports it as a series of images",
"author": "JayGee0",
"authorUrl": "https://github.com/JayGee0",
"isDesktopOnly": false
}

0 comments on commit eef0787

Please sign in to comment.