Initial commit

This commit is contained in:
GnoxNahte 2024-03-05 08:42:56 +08:00
commit 529c6c2b77
18 changed files with 2560 additions and 0 deletions

10
.editorconfig Normal file
View file

@ -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
.eslintignore Normal file
View file

@ -0,0 +1,3 @@
node_modules/
main.js

23
.eslintrc Normal file
View file

@ -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"
}
}

2
.gitattributes vendored Normal file
View file

@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

22
.gitignore vendored Normal file
View file

@ -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
.npmrc Normal file
View file

@ -0,0 +1 @@
tag-version-prefix=""

2
README.md Normal file
View file

@ -0,0 +1,2 @@
# obsidian-auto-embed
Obsidian plugin to help embed links using markdown instead of iframe

25
embeds/codepen.ts Normal file
View file

@ -0,0 +1,25 @@
import { PluginSettings } from "main";
import { EmbedBase } from "./embedBase";
export class CodepenEmbed implements EmbedBase {
name = "CodePen";
regex = new RegExp(/https:\/\/codepen\.io\/(\w+)\/(?:pen)\/(\w+)/);
createEmbed(link: string, container: HTMLElement, settings: Readonly<PluginSettings>): HTMLElement {
const iframe = createEl("iframe", {parent: container});
let url = link;
if (link.contains("?"))
url = link.substring(0, link.indexOf("?"));
if (link.contains("/pen/"))
url = url.replace("/pen/", "/embed/");
iframe.src = url + "?default-tab=result&editable=true";
// iframe.href = url + "?default-tab=result&editable=true";
iframe.textContent = "Codepen";
container.appendChild(iframe);
container.classList.add("codepen");
return iframe;
}
}

13
embeds/embedBase.ts Normal file
View file

@ -0,0 +1,13 @@
import { PluginSettings } from "main";
export interface EmbedBase {
readonly name: string;
// To identify if the anchor link matches the embed type
readonly regex: RegExp;
createEmbed(
link: string,
container: HTMLElement,
settings: Readonly<PluginSettings>,
): HTMLElement;
}

48
esbuild.config.mjs Normal file
View file

@ -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();
}

82
main.ts Normal file
View file

@ -0,0 +1,82 @@
import { CodepenEmbed } from 'embeds/codepen';
import { EmbedBase } from 'embeds/embedBase';
import { Plugin } from 'obsidian';
// Remember to rename these classes and interfaces!
export interface PluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: PluginSettings = {
mySetting: 'default'
}
export default class MyPlugin extends Plugin {
settings: PluginSettings;
embedSources: EmbedBase[] = [
new CodepenEmbed(),
]
async onload() {
console.log('loading plugin!!')
await this.loadSettings();
this.registerMarkdownPostProcessor((el, ctx) => {
console.log("Registering markdown")
const anchors = el.querySelectorAll('a.external-link') as NodeListOf<HTMLAnchorElement>;
anchors.forEach((anchor) => {
console.log("Testing: " + anchor.text);
this.handleAnchor(anchor);
})
})
}
onunload() {
console.log('unloading plugin')
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
private handleAnchor(a: HTMLAnchorElement) {
const innerText = a.innerText;
// Removes all spaces
const innerTextTrim = innerText.replace(/\s/g, "");
if (innerTextTrim.includes("|noembed")) {
return;
}
const href = a.href;
console.log("Testing: " + href);
const embedSource = this.embedSources.find((source) => {
return source.regex.test(href);
})
if (embedSource === undefined) {
return;
}
console.log("Found! : " + href);
const embed = this.createEmbed(embedSource, href);
this.insertEmbed(a, embed);
}
private createEmbed(embedSource: EmbedBase, link: string) {
const container = createDiv({cls: "embed-container"});
const embed = embedSource.createEmbed(link, container, this.settings);
return embed;
return container;
}
private insertEmbed(a: HTMLAnchorElement, container: HTMLElement) {
const parent = a.parentElement;
parent?.replaceChild(container, a);
}
}

11
manifest.json Normal file
View file

@ -0,0 +1,11 @@
{
"id": "auto-embed",
"name": "Auto Embed",
"version": "1.0.0",
"minAppVersion": "0.15.0",
"description": "Helps to embed links using markdown instead of iframe",
"author": "GnoxNahte",
"authorUrl": "gnoxnahte.com",
"fundingUrl": "https://ko-fi.com/gnoxnahtedev",
"isDesktopOnly": false
}

2229
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

24
package.json Normal file
View file

@ -0,0 +1,24 @@
{
"name": "obsidian-sample-plugin",
"version": "1.0.0",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.17.3",
"obsidian": "latest",
"tslib": "2.4.0",
"typescript": "4.7.4"
}
}

24
styles.css Normal file
View file

@ -0,0 +1,24 @@
/*
This CSS file will be included with your plugin, and
available in the app when your plugin is enabled.
If your plugin does not need CSS, delete this file.
*/
iframe {
width: 100%;
min-height: 500px;
}
iframe.small {
min-height: 300px;
}
iframe.large {
min-height: 1000px;
}
iframe.largest {
min-height: calc(100vh - 100px);
}

24
tsconfig.json Normal file
View file

@ -0,0 +1,24 @@
{
"compilerOptions": {
"baseUrl": ".",
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "ES6",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"isolatedModules": true,
"strictNullChecks": true,
"lib": [
"DOM",
"ES5",
"ES6",
"ES7"
]
},
"include": [
"**/*.ts"
]
}

14
version-bump.mjs Normal file
View file

@ -0,0 +1,14 @@
import { readFileSync, writeFileSync } from "fs";
const targetVersion = process.env.npm_package_version;
// read minAppVersion from manifest.json and bump version to target version
let manifest = JSON.parse(readFileSync("manifest.json", "utf8"));
const { minAppVersion } = manifest;
manifest.version = targetVersion;
writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t"));
// update versions.json with target version and minAppVersion from manifest.json
let versions = JSON.parse(readFileSync("versions.json", "utf8"));
versions[targetVersion] = minAppVersion;
writeFileSync("versions.json", JSON.stringify(versions, null, "\t"));

3
versions.json Normal file
View file

@ -0,0 +1,3 @@
{
"1.0.0": "0.15.0"
}