From c0eb499b41c7091b42b2d3618826db7a7848082f Mon Sep 17 00:00:00 2001 From: Matthew Chan <> Date: Sat, 22 May 2021 13:43:33 -0700 Subject: [PATCH] collapse-urls replaces url strings with icons. --- .gitignore | 7 ++++++ README.md | 1 + main.ts | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 8 +++++++ package.json | 20 ++++++++++++++++ rollup.config.js | 19 +++++++++++++++ styles.css | 3 +++ tsconfig.json | 22 +++++++++++++++++ 8 files changed, 141 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.ts create mode 100644 manifest.json create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 styles.css create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30af395 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# npm +node_modules +package-lock.json + +# build +main.js +*.js.map diff --git a/README.md b/README.md new file mode 100644 index 0000000..02dcbcf --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# collapse-urls diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..b1dc85c --- /dev/null +++ b/main.ts @@ -0,0 +1,61 @@ +import {App, MarkdownView, Plugin} from 'obsidian'; + +function makeWidget(): HTMLElement { + let img = document.createElement('img'); + img.src = 'public/images/874d8b8e340f75575caa.svg'; + let widget = document.createElement('span'); + widget.appendChild(img); + return widget; +} + +export default class HideUrls extends Plugin { + collapseUrls(cm: CodeMirror.Editor, line: number) { + let tokens = cm.getLineTokens(line); + for (let token of tokens) { + if (token && token.type && token.type.includes("url") && + token.type.includes("string") && !token.type.includes("formatting")) { + let start = {line : line, ch : token.start}; + let end = {line : line, ch : token.end}; + if (cm.findMarksAt(start).length > 0) { + continue; + } + let widget = makeWidget(); + let marker = cm.markText(start, end, + {replacedWith : widget, clearOnEnter : true}); + widget.onclick = (e) => { + cm.setSelection(marker.find().from, marker.find().to); + marker.clear(); + cm.focus(); + }; + } + } + } + + collapseAllUrls(cm: CodeMirror.Editor) { + for (let line = 0; line < cm.getDoc().lineCount(); line++) { + this.collapseUrls(cm, line); + } + } + + async onload() { + this.app.workspace.on('file-open', () => { + let view = this.app.workspace.activeLeaf.view; + if (view instanceof MarkdownView) { + let cm = view.sourceMode.cmEditor; + this.collapseAllUrls(cm); + } + }); + + this.registerCodeMirror((cm: CodeMirror.Editor) => { + cm.on("cursorActivity", (instance: any) => { + let current_line_number = cm.getCursor().line; + if (current_line_number != this.prev_line_number) { + this.collapseUrls(cm, this.prev_line_number); + this.prev_line_number = current_line_number; + } + }); + }); + } + + prev_line_number: number = 0; +} diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..f06ccdf --- /dev/null +++ b/manifest.json @@ -0,0 +1,8 @@ +{ + "id": "collapse-urls", + "name": "Collapse URLs", + "version": "1.0.0", + "description": "", + "author": "matthewhchan", + "isDesktopOnly": false +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f1229e7 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "main": "main.js", + "scripts": { + "dev": "rollup --config rollup.config.js -w", + "build": "rollup --config rollup.config.js" + }, + "keywords": [], + "author": "", + "license": "MIT", + "devDependencies": { + "@rollup/plugin-commonjs": "^15.1.0", + "@rollup/plugin-node-resolve": "^9.0.0", + "@rollup/plugin-typescript": "^6.0.0", + "@types/node": "^14.14.2", + "obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master", + "rollup": "^2.32.1", + "tslib": "^2.0.3", + "typescript": "^4.0.3" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..4f6107e --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,19 @@ +import typescript from '@rollup/plugin-typescript'; +import {nodeResolve} from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; + +export default { + input: 'main.ts', + output: { + dir: '.', + sourcemap: 'inline', + format: 'cjs', + exports: 'default' + }, + external: ['obsidian'], + plugins: [ + typescript(), + nodeResolve({browser: true}), + commonjs(), + ] +}; \ No newline at end of file diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..9ba3c52 --- /dev/null +++ b/styles.css @@ -0,0 +1,3 @@ +.cm-formatting.cm-formatting-link-string.cm-string.cm-url { + text-decoration: none; +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..09cf7ec --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "inlineSourceMap": true, + "inlineSources": true, + "module": "ESNext", + "target": "es6", + "allowJs": true, + "noImplicitAny": true, + "moduleResolution": "node", + "importHelpers": true, + "lib": [ + "dom", + "es5", + "scripthost", + "es2015" + ] + }, + "include": [ + "**/*.ts" + ] +}