From 71ae9d2bf17fe3a435b4522497845dfe004bec0e Mon Sep 17 00:00:00 2001 From: Luca Orio Date: Fri, 20 May 2022 01:25:42 -0700 Subject: [PATCH] introduced yaml formatting, improved error check --- package-lock.json | 21 +++++++++++++++++++++ package.json | 2 ++ src/img-gallery-renderer.ts | 26 ++++++++++++++------------ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index b878233..a571eb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -82,6 +82,12 @@ "integrity": "sha512-CuPgU6f3eT/XgKKPqKd/gLZV1Xmvf1a2R5POBOGQa6uv82xpls89HU5zKeVoyR8XzHd1RGNOlQlvUe3CFkjWNQ==", "dev": true }, + "@types/js-yaml": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.5.tgz", + "integrity": "sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==", + "dev": true + }, "@types/json-schema": { "version": "7.0.11", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz", @@ -198,6 +204,12 @@ "eslint-visitor-keys": "^3.3.0" } }, + "argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", + "dev": true + }, "array-union": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", @@ -524,6 +536,15 @@ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "dev": true }, + "js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dev": true, + "requires": { + "argparse": "^2.0.1" + } + }, "lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", diff --git a/package.json b/package.json index 89b2018..3d88932 100644 --- a/package.json +++ b/package.json @@ -23,11 +23,13 @@ ], "license": "MIT", "devDependencies": { + "@types/js-yaml": "^4.0.5", "@types/node": "^16.11.6", "@typescript-eslint/eslint-plugin": "^5.2.0", "@typescript-eslint/parser": "^5.2.0", "builtin-modules": "^3.2.0", "esbuild": "0.13.12", + "js-yaml": "^4.1.0", "obsidian": "latest", "tslib": "2.3.1", "typescript": "^4.4.4" diff --git a/src/img-gallery-renderer.ts b/src/img-gallery-renderer.ts index 280f95f..35a2403 100644 --- a/src/img-gallery-renderer.ts +++ b/src/img-gallery-renderer.ts @@ -1,4 +1,5 @@ import { App, MarkdownRenderChild, TFolder, TFile, Platform, normalizePath } from 'obsidian' +import * as jsyaml from 'js-yaml' import ImgGallery from './main' export class imgGalleryRenderer extends MarkdownRenderChild { @@ -25,26 +26,27 @@ export class imgGalleryRenderer extends MarkdownRenderChild { } async onunload() { - this._gallery.remove() - this._gallery = null + if (this._gallery) { + this._gallery.remove() + this._gallery = null + } } private _getSettings() { // parse the settings from the code block - const settingsObj: {[key: string]: string} = {} - - this.src.split('\n') - .filter((row) => row.length > 0) - .forEach((item) => { - const setting = item.split('=') - settingsObj[setting[0]] = setting[1] - }) + const settingsObj: any = jsyaml.load(this.src) // check for required settings + if (settingsObj === undefined) { + const error = 'Cannot parse YAML!' + this._renderError(error) + throw new Error(error) + } + if (!settingsObj.path) { const error = 'Please specify a path!' this._renderError(error) - throw new Error(error); + throw new Error(error) } // store settings, normalize and set sensible defaults @@ -74,7 +76,7 @@ export class imgGalleryRenderer extends MarkdownRenderChild { else { const error = 'The folder doesn\'t exist, or it\'s empty!' this._renderError(error) - throw new Error(error); + throw new Error(error) } // filter the list of files to make sure we're dealing with images only