unverbuggt / mkdocs-encryptcontent-plugin

A MkDocs plugin that encrypt/decrypt markdown content with AES
https://unverbuggt.github.io/mkdocs-encryptcontent-plugin/
MIT License
123 stars 15 forks source link

Updated the patch for material for mkdocs v9.4+ #57

Closed deponovo closed 9 months ago

deponovo commented 9 months ago

Closes #56

unverbuggt commented 9 months ago

Hi,

I've tested your patch in material 9.5.2, but it doesn't seem to work correctly (produces errors in javascript window). Maybe we need to copy the return request(url, options) part of the original source to the } else { part. What I mean is:

this:

  } else {
    return request(url, options)
      .pipe(
        switchMap(res => res.text()),
        map(body => JSON.parse(body) as T),
        shareReplay(1)
      )
  }

instead of that:

  } else {
    return request(url, options)
      .pipe(
        switchMap(res => res.json()),
        shareReplay(1)
      )
  }

It would be good to also check out @ttran2's patches. Maybe they are a better/cleaner way, but I didn't dive into it, yet.

deponovo commented 9 months ago

I am not a javascript developer. I can try your suggestion, but I can't review the patches from ttran2.

unverbuggt commented 9 months ago

The abomination of a language in which material is coded is called typescript. Looks like we need to mimic whatever map(body => JSON.parse(body) as T), does...

unverbuggt commented 9 months ago

This patch fixes it for me, please test and add this to your pull request:

--- src/templates/assets/javascripts/browser/request/index.ts   2023-12-11 16:21:03.000000000 +0100
+++ src/templates/assets/javascripts/browser/request/index.ts.encryptcontent    2023-12-16 19:57:45.611514178 +0100
@@ -24,6 +24,9 @@
   Observable,
   Subject,
   map,
+  tap,
+  of,
+  throwError,
   shareReplay,
   switchMap
 } from "rxjs"
@@ -117,12 +120,41 @@
 export function requestJSON<T>(
   url: URL | string, options?: Options
 ): Observable<T> {
-  return request(url, options)
-    .pipe(
-      switchMap(res => res.text()),
-      map(body => JSON.parse(body) as T),
-      shareReplay(1)
-    )
+  /* Hack to enable search Index decryption */
+  if (url.href.endsWith('/search_index.json')) {
+    const encryptcontentIndexKey = 'encryptcontent-index';
+    const encryptcontentIndex = sessionStorage.getItem(encryptcontentIndexKey);
+
+    if (encryptcontentIndex) {
+      // if already available in sessionStorage return Observable JSON data
+      try {
+        const parsedData = JSON.parse(encryptcontentIndex) as T;
+        return of(parsedData);
+      } catch (error) {
+        console.error('Error parsing encryptcontent-index:', error);
+        return throwError(error);
+      }
+    } else {
+      // if not yet available in sessionStorage request JSON data
+      return request(url, options).pipe(
+        switchMap(res => res.text()),
+        map(body => JSON.parse(body) as T),
+        tap((jsonData) => {
+          // and save them to sessionstorage
+          sessionStorage.setItem(encryptcontentIndexKey, JSON.stringify(jsonData));
+        }),
+        shareReplay(1)
+      );
+    }
+  } else {
+    return request(url, options)
+      .pipe(
+        switchMap(res => res.text()),
+        map(body => JSON.parse(body) as T),
+        shareReplay(1)
+      )
+  }
 }

 /**
deponovo commented 9 months ago

Tested and working