Skip to content

Commit

Permalink
feat: support codeblock
Browse files Browse the repository at this point in the history
  • Loading branch information
Postcount CI committed Jun 13, 2023
1 parent ccef043 commit 7cdd326
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"trailingComma": "none",
"tabWidth": 4,
"semi": false,
"printWidth": 180,
"singleQuote": true
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/MCPlugin.ts
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 13 additions & 1 deletion src/fns/registerCodeBlockProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 11 additions & 7 deletions src/fns/registerLinkProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,6 +38,8 @@ export default class OpenGatePlugin extends Plugin {

registerCodeBlockProcessor(this)
registerLinkProcessor(this)

this.registerEditorExtension([examplePlugin])
}

private async initFrames() {
Expand Down

0 comments on commit 7cdd326

Please sign in to comment.