Skip to content

Commit

Permalink
collapse-urls replaces url strings with icons.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Chan committed May 22, 2021
0 parents commit c0eb499
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# npm
node_modules
package-lock.json

# build
main.js
*.js.map
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# collapse-urls
61 changes: 61 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "collapse-urls",
"name": "Collapse URLs",
"version": "1.0.0",
"description": "",
"author": "matthewhchan",
"isDesktopOnly": false
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
19 changes: 19 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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(),
]
};
3 changes: 3 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.cm-formatting.cm-formatting-link-string.cm-string.cm-url {
text-decoration: none;
}
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit c0eb499

Please sign in to comment.