From 7cdd326c955450e91725d97cd77b770e4062d6cc Mon Sep 17 00:00:00 2001 From: Postcount CI Date: Tue, 13 Jun 2023 11:51:50 +0700 Subject: [PATCH] feat: support codeblock --- .prettierrc | 1 + package.json | 3 ++- pnpm-lock.yaml | 9 ++++++--- src/MCPlugin.ts | 16 ++++++++++++++++ src/fns/registerCodeBlockProcessor.ts | 14 +++++++++++++- src/fns/registerLinkProcessor.ts | 18 +++++++++++------- src/main.ts | 3 +++ 7 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 src/MCPlugin.ts diff --git a/.prettierrc b/.prettierrc index 3cccc47..7b2ee50 100644 --- a/.prettierrc +++ b/.prettierrc @@ -2,5 +2,6 @@ "trailingComma": "none", "tabWidth": 4, "semi": false, + "printWidth": 180, "singleQuote": true } diff --git a/package.json b/package.json index 91dd801..c41fdb9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-open-gate", - "version": "1.8.1", + "version": "1.8.2", "description": "Embedding any website to Obsidian, from now all, you have anything you need in one place.", "main": "main.js", "scripts": { @@ -13,6 +13,7 @@ "author": "", "license": "MIT", "devDependencies": { + "@codemirror/view": "^6.12.0", "@types/chrome": "^0.0.203", "@types/node": "^16.11.6", "@typescript-eslint/eslint-plugin": "5.29.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bd41c14..4d03dd5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,6 +1,9 @@ lockfileVersion: '6.0' devDependencies: + '@codemirror/view': + specifier: ^6.12.0 + version: 6.12.0 '@types/chrome': specifier: ^0.0.203 version: 0.0.203 @@ -24,7 +27,7 @@ devDependencies: version: 0.14.47 obsidian: specifier: latest - version: 1.1.1(@codemirror/state@6.2.1)(@codemirror/view@6.12.0) + version: 1.2.8(@codemirror/state@6.2.1)(@codemirror/view@6.12.0) prettier: specifier: ^2.8.1 version: 2.8.1 @@ -1354,8 +1357,8 @@ packages: dev: true optional: true - /obsidian@1.1.1(@codemirror/state@6.2.1)(@codemirror/view@6.12.0): - resolution: {integrity: sha512-GcxhsHNkPEkwHEjeyitfYNBcQuYGeAHFs1pEpZIv0CnzSfui8p8bPLm2YKLgcg20B764770B1sYGtxCvk9ptxg==} + /obsidian@1.2.8(@codemirror/state@6.2.1)(@codemirror/view@6.12.0): + resolution: {integrity: sha512-HrC+feA8o0tXspj4lEAqxb1btwLwHD2oHXSwbbN+CdRHURqbCkuIDLld+nkuyJ1w1c9uvVDRVk8BoeOnWheOrQ==} peerDependencies: '@codemirror/state': ^6.0.0 '@codemirror/view': ^6.0.0 diff --git a/src/MCPlugin.ts b/src/MCPlugin.ts new file mode 100644 index 0000000..9ef9901 --- /dev/null +++ b/src/MCPlugin.ts @@ -0,0 +1,16 @@ +import { ViewUpdate, PluginValue, EditorView, ViewPlugin } from '@codemirror/view' + +class ExamplePlugin implements PluginValue { + private dom: HTMLDivElement | null = null + constructor(view: EditorView) { + console.log(view.dom.getElementsByTagName('img')) + } + + update(update: ViewUpdate) {} + + destroy() { + this.dom?.remove() + } +} + +export const examplePlugin = ViewPlugin.fromClass(ExamplePlugin) diff --git a/src/fns/registerCodeBlockProcessor.ts b/src/fns/registerCodeBlockProcessor.ts index b8eb329..126927f 100644 --- a/src/fns/registerCodeBlockProcessor.ts +++ b/src/fns/registerCodeBlockProcessor.ts @@ -18,21 +18,33 @@ export function registerCodeBlockProcessor(plugin: Plugin) { let src = '' let height = 'fit-content' let profileKey = 'open-gate' + let userAgent = '' + let zoomFactor = 1 for (const line of lines) { if (line.startsWith('http')) { src = line.trim() } else if (line.startsWith('height:')) { height = line.replace('height:', '').trim() + // if height is a number, add px + if (!isNaN(Number(height))) { + height = height + 'px' + } } else if (line.startsWith('profile:')) { profileKey = line.replace('profile:', '').trim() + } else if (line.startsWith('useragent:')) { + userAgent = line.replace('useragent:', '').trim() + } else if (line.startsWith('zoom:')) { + zoomFactor = parseFloat(line.replace('zoom:', '').trim()) } } let frame: HTMLIFrameElement | WebviewTag const options = { profileKey: profileKey, - url: src + url: src, + userAgent: userAgent, + zoomFactor: zoomFactor } if (Platform.isMobileApp) { diff --git a/src/fns/registerLinkProcessor.ts b/src/fns/registerLinkProcessor.ts index c7770c3..4be7a82 100644 --- a/src/fns/registerLinkProcessor.ts +++ b/src/fns/registerLinkProcessor.ts @@ -15,12 +15,14 @@ export const registerLinkProcessor = (plugin: Plugin) => { const alt = el.getAttribute('alt') const altArr = alt?.split(';') - const height = altArr - ? altArr[0].replace('height:', '')?.trim() ?? '400px' - : '400px' - const profileKey = altArr - ? altArr[1]?.replace('profile:', '') - : 'open-gate' + let height = altArr ? altArr[0].replace('height:', '')?.trim() ?? '400px' : '400px' + if (!isNaN(Number(height))) { + height = height + 'px' + } + + const profileKey = altArr ? altArr[1]?.replace('profile:', '') : 'open-gate' + const useragent = altArr ? altArr[2]?.replace('useragent:', '') : '' + const zoomFactor = altArr ? parseFloat(altArr[3]?.replace('zoom:', '') ?? '1') : 1 if (!src || isImageExt(src)) { return @@ -29,7 +31,9 @@ export const registerLinkProcessor = (plugin: Plugin) => { let frame: HTMLIFrameElement | WebviewTag const options = { profileKey: profileKey, - url: src + url: src, + userAgent: useragent, + zoomFactor: zoomFactor } if (Platform.isMobileApp) { diff --git a/src/main.ts b/src/main.ts index 37832f1..09c9246 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ import { normalizeGateOption } from './fns/normalizeGateOption' import { ModalListGates } from './ModalListGates' import { registerCodeBlockProcessor } from './fns/registerCodeBlockProcessor' import { registerLinkProcessor } from './fns/registerLinkProcessor' +import { examplePlugin } from './MCPlugin' interface PluginSetting { uuid: string @@ -37,6 +38,8 @@ export default class OpenGatePlugin extends Plugin { registerCodeBlockProcessor(this) registerLinkProcessor(this) + + this.registerEditorExtension([examplePlugin]) } private async initFrames() {