Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
meld-cp committed Mar 10, 2024
2 parents 9fa4f3a + 18a6c11 commit 40b18b8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 38 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ Thank you for your support 😊

Report any bugs or feature requests [here](https://github.com/meld-cp/obsidian-encrypt/issues).

## v2.3.7
- Add a setting to remember session passwords by vault ([#146](https://github.com/meld-cp/obsidian-encrypt/issues/146), [#149](https://github.com/meld-cp/obsidian-encrypt/issues/149))
- Add a command to clear session password cache
- Clarify 'Remember By' description in settings ([#146](https://github.com/meld-cp/obsidian-encrypt/issues/146), [#149](https://github.com/meld-cp/obsidian-encrypt/issues/149))
- Add ribbon icon for 'Encrypt/Decrypt' command ([#157](https://github.com/meld-cp/obsidian-encrypt/issues/157))
- Fix missing lock indicator in reading view ([#156](https://github.com/meld-cp/obsidian-encrypt/issues/156))
- Bug fixes

## v2.3.6
- Fix internal links not working in an encrypted note ([#144](https://github.com/meld-cp/obsidian-encrypt/issues/144))
- Add 'New encrypted note' option to folder context menu ([#106](https://github.com/meld-cp/obsidian-encrypt/issues/106))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ OR
| Confirm password | Confirm password when encrypting (recommended) |
| Remember password | Remember the last used password for this session. |
| Remember Password Timeout | The number of minutes to remember the last used password. |
| Remember Password Using | Remember passwords by using `File Name` or `Parent Folder` matching |
| Remember Password Using | Remember session passwords by using `Vault`, `Folder` or `File` matching |

### Whole note encryption
| | |
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": "meld-encrypt",
"name": "Meld Encrypt",
"version": "2.3.6",
"version": "2.3.7",
"minAppVersion": "1.0.3",
"description": "Hide secrets in your vault",
"author": "meld-cp",
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-sample-plugin",
"version": "2.3.6",
"version": "2.3.7",
"description": "Hide secrets in your vault",
"main": "main.js",
"scripts": {
Expand Down
73 changes: 49 additions & 24 deletions src/features/feature-inplace-encrypt/FeatureInplaceEncrypt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Editor, EditorPosition, Notice, Setting, MarkdownPostProcessorContext } from "obsidian";
import { Editor, EditorPosition, Notice, Setting, MarkdownPostProcessorContext, MarkdownView } from "obsidian";
import DecryptModal from "./DecryptModal";
import { IMeldEncryptPluginFeature } from "../IMeldEncryptPluginFeature";
import MeldEncrypt from "../../main";
Expand Down Expand Up @@ -33,6 +33,19 @@ export default class FeatureInplaceEncrypt implements IMeldEncryptPluginFeature{
icon: 'lock',
editorCheckCallback: (checking, editor, view) => this.processEncryptDecryptCommand( checking, editor, false )
});

this.plugin.addRibbonIcon(
'file-lock',
'Encrypt/Decrypt',
(_) => {
const activeView = this.plugin.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView == null ){
console.debug('no active view found');
return;
}
return this.processEncryptDecryptCommand(false, activeView.editor, false);
}
);

plugin.addCommand({
id: 'meld-encrypt-in-place',
Expand All @@ -47,50 +60,62 @@ export default class FeatureInplaceEncrypt implements IMeldEncryptPluginFeature{

}

private replaceMarkersRecursive( node: Node ) {
private replaceMarkersRecursive( node: Node, rlevel: number = 0 ) : Node[] {

if ( node instanceof HTMLElement ){
node.childNodes.forEach( n => this.replaceMarkersRecursive(n) );
return;
for( const n of Array.from(node.childNodes) ){
var childNodes = this.replaceMarkersRecursive( n, rlevel+1 );
n.replaceWith( ...childNodes );
}
return [node];
}

if ( node instanceof Text ){

const text = node.textContent;

if ( text == null ){
return;
return [node];
}

if ( !text.contains( '🔐' ) ){
return;
return [node];
}

const parent = node.parentElement;
if ( parent == null ){
return;
}


const reInplaceMatcher = /🔐(.*?)🔐/g;

const splits = text.split( reInplaceMatcher );

parent.removeChild( node );

for ( const markerMatch of text.matchAll( reInplaceMatcher ) ) {
parent.createSpan( {
cls: 'meld-encrypt-inline-reading-marker',
text : '🔐',
attr : {
'data-meld-encrypt-encrypted' : markerMatch[0]
}
} );
const nodes : Node[] = [];

for (let i = 0; i < splits.length; i++) {
const t = splits[i];
if ( i % 2 != 0 ){
// odd indexes have indicators
const node = createSpan({
cls: 'meld-encrypt-inline-reading-marker',
text: '🔐',
attr: {
'data-meld-encrypt-encrypted' : `🔐${t}🔐`
}
})
nodes.push( node );
} else {
nodes.push( new Text( t ) );
}
}

return nodes;

}

return [node];
}

private async processEncryptedCodeBlockProcessor(el: HTMLElement, ctx: MarkdownPostProcessorContext){
this.replaceMarkersRecursive(el);
const replacementNodes = this.replaceMarkersRecursive(el);
//console.debug( 'processEncryptedCodeBlockProcessor', { el, replacementNodes } );
el.replaceWith( ...replacementNodes );
// bind events
const elIndicators = el.querySelectorAll('.meld-encrypt-inline-reading-marker');
this.bindReadingIndicatorEventHandlers( ctx.sourcePath, elIndicators );
Expand Down
14 changes: 12 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin } from 'obsidian';
import { Notice, Plugin } from 'obsidian';
import MeldEncryptSettingsTab from './settings/MeldEncryptSettingsTab';
import { IMeldEncryptPluginSettings } from './settings/MeldEncryptPluginSettings';
import { IMeldEncryptPluginFeature } from './features/IMeldEncryptPluginFeature';
Expand Down Expand Up @@ -35,6 +35,16 @@ export default class MeldEncrypt extends Plugin {
);
// End Settings

this.addCommand({
id: 'meld-encrypt-clear-password-cache',
name: 'Clear Session Password Cache',
icon: 'file-lock',
callback: () => {
const itemsCleared = SessionPasswordService.clear();
new Notice( `Items cleared: ${itemsCleared}` );
},
});

// load features
this.enabledFeatures.forEach(async f => {
await f.onload( this, this.settings );
Expand All @@ -54,7 +64,7 @@ export default class MeldEncrypt extends Plugin {
confirmPassword: true,
rememberPassword: true,
rememberPasswordTimeout: 30,
rememberPasswordLevel: SessionPasswordService.LevelFilename,
rememberPasswordLevel: SessionPasswordService.LevelVault,

featureWholeNoteEncrypt: {
defaultView: EditViewEnum.source.toString()
Expand Down
30 changes: 25 additions & 5 deletions src/services/SessionPasswordService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export class SessionPasswordService{

public static LevelFilename = 'filename';
public static LevelParentPath = 'parentPath';
public static LevelVault = 'vault';
private static allLevels = [
SessionPasswordService.LevelFilename,
SessionPasswordService.LevelParentPath
SessionPasswordService.LevelParentPath,
SessionPasswordService.LevelVault,
];
private static level = SessionPasswordService.LevelFilename;
private static level = SessionPasswordService.LevelVault;

public static setActive( isActive: boolean) {
SessionPasswordService.isActive = isActive;
Expand Down Expand Up @@ -111,26 +113,42 @@ export class SessionPasswordService{
}

private static getPathCacheKey( path : string ) : string {
//console.debug('getPathCacheKey', {path});

const parentPath = path.split('/').slice(0,-1).join('/');
//console.debug({path,parentPath, filepath: app.workspace.getActiveFile()});

switch (SessionPasswordService.level) {
case SessionPasswordService.LevelVault: {
//console.debug('getPathCacheKey: $vault');
return '$vault';
}

case SessionPasswordService.LevelParentPath: {
//console.debug('getPathCacheKey: ', parentPath);
return parentPath;
}

default:
//console.debug('getPathCacheKey: ', path);
return path;
}
}

private static getFileCacheKey( file : TFile ) : string {
//console.debug('getFileCacheKey', {file});
switch (SessionPasswordService.level) {
case SessionPasswordService.LevelVault: {
//console.debug('getFileCacheKey: $vault');
return '$vault';
}
case SessionPasswordService.LevelParentPath: {
//console.debug('getFileCacheKey:', file.parent.path);
return file.parent.path;
}
default:
return Utils.getFilePathExcludingExtension( file );
const fileExExt = Utils.getFilePathExcludingExtension( file );
//console.debug('getFileCacheKey:', fileExExt);
return fileExExt;
}
}

Expand All @@ -154,8 +172,10 @@ export class SessionPasswordService{
this.cache.removeKey( key );
}

public static clear(): void{
public static clear(): number {
const count = this.cache.getKeys().length;
this.cache.clear();
return count;
}

}
Expand Down
28 changes: 25 additions & 3 deletions src/settings/MeldEncryptSettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ export default class MeldEncryptSettingsTab extends PluginSettingTab {
;

const rememberPasswordLevelSetting = new Setting(containerEl)
.setDesc('Remember passwords by using a notes file name or parent folder')
.setName('Remember passwords by:')
.setDesc( this.buildRememberPasswordDescription() )
.addDropdown( cb =>{
cb
.addOption( SessionPasswordService.LevelFilename, 'File Name')
.addOption( SessionPasswordService.LevelParentPath, 'Parent Folder')
.addOption( SessionPasswordService.LevelVault, 'Vault')
.addOption( SessionPasswordService.LevelParentPath, 'Folder')
.addOption( SessionPasswordService.LevelFilename, 'File')
.setValue( this.settings.rememberPasswordLevel )
.onChange( async value => {
this.settings.rememberPasswordLevel = value;
Expand All @@ -120,4 +122,24 @@ export default class MeldEncryptSettingsTab extends PluginSettingTab {

}

private buildRememberPasswordDescription( ) : DocumentFragment {
const f = new DocumentFragment();

const tbody = f.createEl( 'table' ).createTBody();

let tr = tbody.createEl( 'tr' );
tr.createEl( 'th', { text: 'Vault:', attr: { 'align': 'left'} });
tr.createEl( 'td', { text: 'typically, you\'ll use the same password every time.' });

tr = tbody.createEl( 'tr' );
tr.createEl( 'th', { text: 'Folder:', attr: { 'align': 'left'} });
tr.createEl( 'td', { text: 'typically, you\'ll use the same password for each note within a folder.' });

tr = tbody.createEl( 'tr' );
tr.createEl( 'th', { text: 'File:', attr: { 'align': 'left'} });
tr.createEl( 'td', { text: 'typically, each note will have a unique password.' });

return f;
}

}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
"2.3.3": "1.0.3",
"2.3.4": "1.0.3",
"2.3.5": "1.0.3",
"2.3.6": "1.0.3"
"2.3.6": "1.0.3",
"2.3.7": "1.0.3"
}

0 comments on commit 40b18b8

Please sign in to comment.