Skip to content

Commit

Permalink
Added ability to escape '<' character when pasting into blockquotes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglev committed May 22, 2022
1 parent 68f83e3 commit 508cf28
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
56 changes: 54 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
Setting,
} from "obsidian";

import { toggleQuote, toggleQuoteInEditor } from "./src/toggle-quote";
import {
escapeRegExp,
toggleQuote,
toggleQuoteInEditor,
} from "./src/toggle-quote";

const moment = require("moment");

Expand Down Expand Up @@ -135,16 +139,20 @@ interface PastetoIndentationPluginSettings {
saveBase64EncodedFiles: boolean;
saveFilesLocation: string;
apiVersion: number;
escapeCharactersInBlockquotes: boolean;
}

const DEFAULT_SETTINGS: PastetoIndentationPluginSettings = {
blockquotePrefix: "> ",
mode: Mode.Markdown,
saveBase64EncodedFiles: false,
saveFilesLocation: "",
apiVersion: 3,
apiVersion: 4,
escapeCharactersInBlockquotes: false,
};

const blockquoteCharactersToEscape = "<";

for (const [key, value] of Object.entries(pluginIcons)) {
addIcon(key, value);
}
Expand Down Expand Up @@ -350,7 +358,34 @@ export default class PastetoIndentationPlugin extends Plugin {
new RegExp(`^${leadingWhitespace}`),
""
);

output = toggledText.lines.join("\n");

if (this.settings.escapeCharactersInBlockquotes) {
const charactersToEscape = [
...output.matchAll(
new RegExp(
`[${escapeRegExp(blockquoteCharactersToEscape)}]`,
"g"
)
),
]
.map((x) => x.index)
.reverse();

charactersToEscape.forEach((index) => {
if (
output[Number(index) - 1] !== "\\" &&
!(
output[Number(index) - 1] === "\\" &&
output[Number(index) - 2] === "\\"
)
) {
output =
output.substring(0, index) + "\\" + output.substring(index);
}
});
}
}

const transaction: EditorTransaction = {
Expand Down Expand Up @@ -651,5 +686,22 @@ class SettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Escape characters in blockquotes")
.setDesc(
`When pasting in Text (Blockquote), Code Block (Blockquote), or Markdown (Blockquote) mode, add a backslash escape character to the beginning of "${blockquoteCharactersToEscape}" characters.`
)
.addToggle((toggle) => {
toggle
.setValue(
this.plugin.settings.escapeCharactersInBlockquotes ||
DEFAULT_SETTINGS.escapeCharactersInBlockquotes
)
.onChange(async (value) => {
this.plugin.settings.escapeCharactersInBlockquotes = value;
await this.plugin.saveSettings();
});
});
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-paste-to-current-indentation",
"name": "Paste to Current Indentation",
"version": "3.0.2",
"version": "3.1.0",
"minAppVersion": "0.14.6",
"description": "This plugin allows pasting and marking text as block-quotes at any level of indentation.",
"author": "Jacob Levernier",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-paste-to-current-indentation",
"version": "3.0.2",
"version": "3.1.0",
"description": "This plugin allows pasting and marking text as block-quotes at any level of indentation.",
"main": "main.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/toggle-quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { MarkdownView } from 'obsidian';
// which, as a code snippet, is in the public domain, per
// https://developer.mozilla.org/en-US/docs/MDN/About#copyrights_and_licenses
// (as of 2021-07-15):
function escapeRegExp(string: string) {
export const escapeRegExp = (string: string) => {
// $& means the whole matched string:
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};

export const toggleQuote = async (
linesInput: string[],
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dom",
"es5",
"scripthost",
"es2020",
"es2021",
"ESNext.String"
]
},
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"2.2.1": "0.14.2",
"3.0.0": "0.14.6",
"3.0.1": "0.14.6",
"3.0.2": "0.14.6"
"3.0.2": "0.14.6",
"3.1.0": "0.14.6"
}

0 comments on commit 508cf28

Please sign in to comment.