Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Rewrite plugin and properly deregister views.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarenholz committed Dec 26, 2021
1 parent f133d68 commit 097df38
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 179 deletions.
47 changes: 36 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
# Plaintext for Obsidian

Obisidan (https://obsidian.md) plugin that allows it to open files as plaintext.
Developed for Obsidian version 0.12.12. It might work with older versions, but it is not tested.
This is an [Obisidan](https://obsidian.md) plugin that allows you to open _any_ file as plaintext. It has been developed for Obsidian **v0.13.14**, and tested on **Windows**.

Code is functional! You can actually edit plaintext files now. There's no fancy syntax highlighting, since the file
is interpreted as plaintext, as opposed to some other type of text.
Honestly, as long as you can run any Obsidian version you can _probably_ run this plugin as well. The only requirements are that we can register extensions (this existed in v0.12.12 for instance), and that the `viewRegistry` exists, which I'm assuming has been there since the beginning of Obsidian. But, this is all speculation!

**NOTE: There are other plugins that allow you to edit specific files. MAKE SURE TO NOT TYPE THEIR EXTENSIONS INTO THE SETTINGS FIELD FOR THIS PLUGIN. I cannot (yet) check for specific plugins that have their own view for a particular extension, and as such this plugin WILL overwrite the view, and break the other extension. If you do this by accident, open the plugin folder (`.obsidian/plugins/obsidian-plaintext/`), and remove from the `data.json` file the extensions that you typed by mistake.**

## Installing

Interested in editing files in Obsidian? Great. Grab the latest release from the [releases](#) page, and copy `main.js` and `manifest.json` to `.obsidian/plugins/obsidian-plaintext/`. That's it!

When approved, you can also install this through Obsidian by searching for **plaintext**.

## Roadmap

For now, nothing is planned. If you're interested in features, please make an issue on Github!

## Contributing

Also excited about making Obsidian a full-fledged IDE? Cool, me too! Contact me and let's talk! Pull requests (especially one that updates the code to use `CodeMirror 6`) are very welcome.

## Pricing

This is free. Keep your money, I don't want it.

## Changelog

**Version 0.0.2 (current)**:
* First actual release.
* Code is functional! You can open and edit files as plaintext.
**Version 0.1.0 (current)**:

- Complete rewrite of registering and deregistering.
- Now _actually_ removes views when deregistering a particular extension.
- Correctly filters out default obsidian extensions: No more accidentally overwriting the default markdown editor.

**Version 0.0.2**:

- First actual release.
- Code is functional! You can open and edit files as plaintext.

**Version 0.0.1**:

**Version 0.0.1**:
* Not a release.
* Initial testing code.
* This included the functionality for parsing user-inputted extensions.
- Not a release.
- Initial testing code.
- This included the functionality for parsing user-inputted extensions.
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "obsidian-plaintext",
"name": "Plaintext",
"version": "0.0.2",
"minAppVersion": "0.12.12",
"description": "Allow opening specified files as plaintext.",
"version": "0.1.0",
"minAppVersion": "0.13.14",
"description": "Allow opening specified files as plaintext (RAW mode).",
"author": "dbarenholz",
"authorUrl": "https://github.com/dbarenholz/dbarenholz",
"isDesktopOnly": true
Expand Down
24 changes: 11 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-plaintext",
"version": "0.0.2",
"version": "0.1.0",
"description": "Allow opening specified files as plaintext.",
"main": "main.js",
"scripts": {
Expand All @@ -10,17 +10,15 @@
"keywords": [],
"author": "dbarenholz",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/node": "^14.14.37",
"obsidian": "^0.12.0",
"rollup": "^2.32.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"dependencies": {
"codemirror": "^5.62.3"
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.1",
"@rollup/plugin-typescript": "^8.3.0",
"@types/node": "^17.0.4",
"codemirror": "^5.65.0",
"obsidian": "^0.13.11",
"rollup": "^2.62.0",
"tslib": "^2.3.1",
"typescript": "^4.5.4"
}
}
}
74 changes: 51 additions & 23 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
// Taken directly from here
// https://help.obsidian.md/Advanced+topics/Accepted+file+formats
// On sept 2, 2021. May need updates later.

export const obsidianExts: string[] = [
/**
* Extensions obsidian supports natively.
* Taken from the help page: https://help.obsidian.md/Advanced+topics/Accepted+file+formats
*
* @version 0.1.0
* @author dbarenholz
* @since 2021/12/26
*/
export const obsidianExts: Set<string> = new Set([
"md",
" png",
" jpg",
" jpeg",
" gif",
" bmp",
" svg",
" mp3",
" webm",
" wav",
" m4a",
" ogg",
" 3gp",
" flac",
" mp4",
" webm",
" ogv",
" pdf",
];
"png",
"jpg",
"jpeg",
"gif",
"bmp",
"svg",
"mp3",
"webm",
"wav",
"m4a",
"ogg",
"3gp",
"flac",
"mp4",
"webm",
"ogv",
"pdf",
]);

/**
* Takes in a list of extensions, and removes extensions if they are present in the obsidianExts set.
*
* @param exts extensions to process
* @returns All extensions in exts, except if they're present in obsidianExts.
*/
export const removeObsidianExtensions = (exts: string[]): string[] => {
return exts.filter(ext => !obsidianExts.has(ext));
}

declare module 'obsidian' {
interface App {
viewRegistry: {
unregisterView: CallableFunction // ƒ (e){delete this.viewByType[e]}
unregisterExtensions: CallableFunction // ƒ (e){for(var t=0,n=e;t<n.length;t++){var i=n[t];delete this.typeByExtension[i]}this.trigger("extensions-updated")}
}
}
interface View {
file: {
extension: string
}
}
}
99 changes: 70 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
import { Plugin, WorkspaceLeaf, ViewCreator } from "obsidian";
import { obsidianExts } from "./helper";
import { removeObsidianExtensions } from "./helper";
import { PlaintextSettings, PlaintextSettingTab, DEFAULT_SETTINGS } from "./settings";
import PlaintextView from "./view";

/**
* Plaintext plugin.
*
* This plugin allows you to edit specified extensions as plaintext files.
* It does NOT check if a file is binary or textual!
* Allows you to edit files with specified extensions as if they are plaintext files.
* There are _absolutely no_ checks to see whether or not you should actually do so.
*
* Use common sense, and don't edit `.exe` or similar binaries.
*
* @author dbarenholz
* @version 0.1.0
*/
export default class PlaintextPlugin extends Plugin {
// The settings of the plugin.
public settings: PlaintextSettings;

/**
* Code that runs (once) when plugin is loaded.
*/
async onload(): Promise<void> {
console.log("Obsidian Plaintext: loaded plugin.");
console.log("[Plaintext]: loaded plugin.");

// Load the settings
await this.loadSettings();

// Add settings tab
this.addSettingTab(new PlaintextSettingTab(this.app, this));

// Do the work
this.processExts(this.settings.extensions);
// Add extensions that we need to add.
this.addExtensions(this.settings.extensions);
}

/**
* Code that runs (once) when the plugin is unloaded.
*/
onunload(): void {
console.log("Obsidian Plaintext: unloaded plugin.");
// cleanup
this.removeExtensions(this.settings.extensions);
console.log("[Plaintext]: unloaded plugin.");
}

/**
Expand All @@ -54,52 +59,88 @@ export default class PlaintextPlugin extends Plugin {

/**
* Creates a view for a plaintext file.
* Plaintext views have <b>NO</b> syntax highlighting or other fancy features!
*
* @param leaf The leaf to create the view at
* @param ext Plaintext extension
* @returns Plaintext view
*/
viewCreator: ViewCreator = (leaf: WorkspaceLeaf, ext?: string): PlaintextView => {
return new PlaintextView(leaf, ext);
viewCreator: ViewCreator = (leaf: WorkspaceLeaf): PlaintextView => {
return new PlaintextView(leaf);
};

/**
* Processes the extensions.
*
* @param exts extensions
* Registers extensions, and makes views for them.
*
* @param exts The extensions to register and add views for.
*/
processExts = (exts: string[]): void => {
if (exts.length == 0) {
console.log("Plaintext: No extensions to process.");
return;
}

for (const ext of exts) {
// Disallow using obsidian defaults
if (ext in obsidianExts) {
if (this.settings.debug) {
console.log(`Plaintext: Extension '${ext}' is used by Obsidian already! Don't override Obsidian.`);
}
}
addExtensions = (exts: string[]): void => {
// Remove obsidian exts just in case
exts = removeObsidianExtensions(exts)

// Loop through extensions
exts.forEach((ext) => {
// Try to register view
try {
this.registerView(ext, this.viewCreator);
} catch {
if (this.settings.debug) {
console.log(`Plaintext: Extension '${ext}' already has a view registered, ignoring...`);
console.log(`[Plaintext]: Extension '${ext}' already has a view registered, ignoring...`);
}
}

// Try to register extension
try {
// Note: viewtype is set to 'ext' here for possible future expansion to include syntax highlighting based on extension type.
this.registerExtensions([ext], ext);
} catch {
if (this.settings.debug) {
console.log(`Plaintext: Extension '${ext}' is already registered, ignoring...`);
console.log(`[Plaintext]: Extension '${ext}' is already registered, ignoring...`);
}
}

// Logging
if (this.settings.debug) {
console.log(`[Plaintext]: added=${ext}`);
}
})
};

/**
* Deregisters extensions, and removes views made for them.
*
* @param exts The extensions to deregister and remove views for.
*/
removeExtensions = (exts: string[]): void => {
// Remove obsidian exts just in case
exts = removeObsidianExtensions(exts)

// Try to deregister the views
exts.forEach((ext) => {
// Before unregistering the view: close active leaf if of type ext
if (ext == this.app.workspace.activeLeaf.view.getViewType()) {
this.app.workspace.activeLeaf.detach();
}

try {
this.app.viewRegistry.unregisterView(ext)
} catch {
if (this.settings.debug) {
console.log(`[Plaintext]: View for extension '${ext}' cannot be deregistered...`);
}
}
});

// Try to deregister the extensions
try {
this.app.viewRegistry.unregisterExtensions(exts)
} catch {
if (this.settings.debug) {
console.log(`[Plaintext]: Cannot deregister extensions...`);
}
}

// Logging
if (this.settings.debug) {
console.log(`[Plaintext]: removed=${exts}`);
}
};
}
Loading

0 comments on commit 097df38

Please sign in to comment.