Skip to content

Commit

Permalink
2.0.0 (#39)
Browse files Browse the repository at this point in the history
- Added commands to use the clipboard.
- Added inline Regex validation in Settings page.
  • Loading branch information
jglev committed Jan 22, 2022
1 parent e7f4c0d commit e7d7ab8
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 92 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ Follow the steps below to install the plugin.

- This plugin uses the [ECMAScript / Javascript flavor](https://www.regular-expressions.info/javascript.html) of Regular Expressions.
- Within a Pattern, rules execute sequentially. Thus, the output of Rule 1 is passed as input to Rule 2, and the output of Rule 2 is passed as input to Rule 3, etc. At the end of the set of rules, the final output is used to replace the text in the editor.
- The plugin provides three commands by default:
- The plugin provides five commands by default:
- `Apply Patterns: Apply pattern to whole lines` will loop over each line that is selected in the editor, and apply the Pattern to the entirety of each line.
- - `Apply Patterns: Apply pattern to whole document` will apply the Pattern to the entire document, as one (potentially multi-line) string.
- `Apply Patterns: Apply pattern to selection` will apply the Pattern to just the text selected in the editor, as one (potentially multi-line) string.
- `Apply Patterns: Apply pattern to whole clipboard` will apply the Pattern as with "`Apply pattern to whole document`" to the clipboard.
- `Apply Patterns: Apply pattern to clipboard (line-by-line)` will apply the Pattern as with "`Apply pattern to whole lines`" to the clipboard.
- In addition, you can set additional commands in the Settings tab.
- Within the Settings tab:
- Each rule can be disabled, moved up, and moved down in the pattern.
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-apply-patterns",
"name": "Apply Patterns",
"version": "1.4.1",
"version": "2.0.0",
"minAppVersion": "0.13.9",
"description": "Apply custom patterns of find-and-replace in succession to text.",
"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-apply-patterns",
"version": "1.4.1",
"version": "2.0.0",
"description": "An Obsidian plugin for applying patterns of find and replace in succession.",
"main": "main.js",
"scripts": {
Expand Down
82 changes: 68 additions & 14 deletions src/ApplyPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { validateRuleString } from './ValidateRuleString';
import { PatternModal } from './PatternsModal';
import { Command, getSettings } from './Settings';
import { cursorTo } from 'readline';

const calculateCursorPoints = (
minLine: number,
Expand Down Expand Up @@ -74,21 +73,18 @@ const calculateCursorPoints = (
};

export const applyPattern = (
checking: boolean,
editor: Editor,
view: View,
app: App,
mode: 'lines' | 'selection' | 'document' = 'lines',
mode:
| 'lines'
| 'selection'
| 'document'
| 'clipboard'
| 'clipboardLines' = 'lines',
command?: Command,
) => {
if (checking) {
// editorCallback always happens in a MarkdownView; the command should
// only be shown in MarkdownView:
return true;
}

if (!(view instanceof MarkdownView)) {
// Should never happen due to check above.
return;
}

Expand All @@ -100,10 +96,6 @@ export const applyPattern = (

const onChooseItem = (patternIndex: number): void => {
const pattern = getSettings().patterns[patternIndex];
const cursorFrom = editor.getCursor('from');
const cursorTo = editor.getCursor('to');
const minLine = cursorFrom.line;
const maxLine = cursorTo.line;

// Confirm that each rule's strings are valid:
let allValid = true;
Expand Down Expand Up @@ -167,6 +159,68 @@ export const applyPattern = (
return; // Stop the function prematurely
}

if (mode === 'clipboard' || mode === 'clipboardLines') {
// This is largely the same as the 'document' mode code, but using
// the clipboard as input.
navigator.clipboard.readText().then((clipboardText) => {
if (mode === 'clipboard') {
pattern.rules.forEach((rule, ruleIndex) => {
clipboardText = clipboardText.replace(
new RegExp(
allRuleStringsValidated[ruleIndex].from,
`u${rule.caseInsensitive ? 'i' : ''}${
rule.global ? 'g' : ''
}${rule.multiline ? 'm' : ''}${
rule.sticky ? 's' : ''
}`,
),
allRuleStringsValidated[ruleIndex].to,
);
});
}
if (mode === 'clipboardLines') {
const clipboardTextSplit = clipboardText.split('\n');
const updatedLines: string[] = [];
for (
let lineNumber = 0;
lineNumber < clipboardTextSplit.length;
lineNumber++
) {
let line = clipboardTextSplit[lineNumber];
pattern.rules.forEach((rule, ruleIndex) => {
if (rule.disabled === true) {
// Skip the rule if it's disabled:
return;
}
line = line.replace(
new RegExp(
allRuleStringsValidated[ruleIndex].from,
`u${rule.caseInsensitive ? 'i' : ''}${
rule.global ? 'g' : ''
}${rule.multiline ? 'm' : ''}${
rule.sticky ? 's' : ''
}`,
),
allRuleStringsValidated[ruleIndex].to,
);
});
updatedLines.push(line);
}
clipboardText = updatedLines.join('\n');
}

navigator.clipboard.writeText(clipboardText);

new Notice('Clipboard updated.');
});
return;
}

const cursorFrom = editor.getCursor('from');
const cursorTo = editor.getCursor('to');
const minLine = cursorFrom.line;
const maxLine = cursorTo.line;

const transaction: EditorTransaction = {
changes: [],
};
Expand Down
4 changes: 3 additions & 1 deletion src/Settings.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export function isCommand(obj: any, _argumentName?: string): obj is Command {
typeof obj.patternFilter === "string" &&
typeof obj.selection === "boolean" &&
typeof obj.lines === "boolean" &&
typeof obj.document === "boolean"
typeof obj.document === "boolean" &&
typeof obj.clipboard === "boolean" &&
typeof obj.clipboardLines === "boolean"
)
}
24 changes: 14 additions & 10 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,23 @@ export const defaultSettings: Settings = {

/** @see {isCommand} ts-auto-guard:type-guard */
export interface Command {
name: string;
patternFilter: string;
selection?: boolean;
lines?: boolean;
document?: boolean;
name: string;
patternFilter: string;
selection?: boolean;
lines?: boolean;
document?: boolean;
clipboard?: boolean;
clipboardLines?: boolean;
}

export const defaultCommandSettings: Command = {
name: '',
patternFilter: '',
selection: true,
lines: true,
document: true,
name: '',
patternFilter: '',
selection: true,
lines: true,
document: true,
clipboard: false,
clipboardLines: false,
};

export const formatUnnamedPattern = (patternIndex: number): string =>
Expand Down
Loading

0 comments on commit e7d7ab8

Please sign in to comment.