Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 82b0a96

Browse files
committed
reusing yaml parser
1 parent f6705af commit 82b0a96

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import { TypeHandler } from 'parsers/embedYamlHandlers/TypeHandler';
88
import { TitleHandler } from 'parsers/embedYamlHandlers/TitleHandler';
99
import { DbFolderError } from "errors/AbstractError";
1010
import { ParserError } from "errors/ParserError";
11+
import { DatabaseYaml } from "cdm/DatabaseModel";
1112
/**
1213
* PUBLIC METHODS
1314
****************/
1415
/**
1516
* Parse a string
1617
*/
17-
export function parseDatabase(yamlText: string, app: App): any {
18+
const DatabaseYamlParser = (yamlText: string): DatabaseYaml => {
1819
const yaml = parseYaml(yamlText);
19-
const errors = validateYaml(yaml, app);
20+
const errors = validateYaml(yaml);
2021
if (errors.length > 0) {
2122
throw new DbFolderError(new ParserError("Error parsing database", errors));
2223
}
@@ -32,14 +33,14 @@ export function parseDatabase(yamlText: string, app: App): any {
3233
/**
3334
* Validate yaml received from input using handlers of function getHandlers
3435
*/
35-
function validateYaml(yaml: any, app: App): [string, string][] {
36+
function validateYaml(yaml: any): [string, string][] {
3637
const handlers = getHandlers();
3738
let i = 1;
3839
while (i < handlers.length) {
3940
handlers[i - 1].setNext(handlers[i]);
4041
i++;
4142
}
42-
return handlers[0].handle(yaml, app);
43+
return handlers[0].handle(yaml);
4344
}
4445

4546

@@ -53,4 +54,7 @@ function getHandlers(): YamlHandler[] {
5354
new FolderHandler(),
5455
new TitleHandler()
5556
];
56-
}
57+
}
58+
59+
// Export
60+
export default DatabaseYamlParser;

src/parsers/embedYamlHandlers/AbstractYamlPropertyHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { App } from "obsidian";
33
export interface YamlHandler {
44
setNext(handler: YamlHandler): YamlHandler;
55

6-
handle(yaml: any, app: App): [string, string][];
6+
handle(yaml: any): [string, string][];
77
}
88

99
export abstract class AbstractYamlHandler implements YamlHandler {
@@ -20,5 +20,5 @@ export abstract class AbstractYamlHandler implements YamlHandler {
2020
this.nextHandler = handler;
2121
return handler;
2222
}
23-
abstract handle(yaml: any, app: App): [string, string][];
23+
abstract handle(yaml: any): [string, string][];
2424
}

src/parsers/embedYamlHandlers/FolderHandler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { AbstractYamlHandler } from 'parsers/embedYamlHandlers/AbstractYamlPrope
22
import { App, normalizePath } from "obsidian";
33
export class FolderHandler extends AbstractYamlHandler {
44
handlerName: string = 'folder';
5-
public handle(yaml: any, app: App): [string, string][] {
5+
public handle(yaml: any): [string, string][] {
66
if (!yaml.folder) {
77
this.addError('Folder name is not defined');
88
// handle is ended if there is no folder name
99
return this.listOfErrors;
1010
}
1111

12-
if (!this.checkIfFolderExist(yaml, app)) {
12+
if (!this.checkIfFolderExist(yaml)) {
1313
// handle is ended if the folder does not exist
1414
return this.listOfErrors;
1515
}
1616
// Check next handler
1717
if (this.nextHandler) {
18-
return this.nextHandler.handle(yaml, app);
18+
return this.nextHandler.handle(yaml);
1919
}
2020
return this.listOfErrors;
2121
}
@@ -28,7 +28,7 @@ export class FolderHandler extends AbstractYamlHandler {
2828
* @param path
2929
* @param folderName
3030
*/
31-
checkIfFolderExist(yaml: any, app: App): boolean {
31+
checkIfFolderExist(yaml: any): boolean {
3232
// check if folder exists
3333
let folder_str = normalizePath(yaml.folder);
3434
const folder = app.vault.getAbstractFileByPath(folder_str);

src/parsers/embedYamlHandlers/TitleHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { App } from "obsidian";
44
export class TitleHandler extends AbstractYamlHandler {
55
handlerName: string = 'title';
66

7-
public handle(yaml: any, app: App): [string, string][]{
7+
public handle(yaml: any): [string, string][]{
88

99
if (!yaml.title || yaml.title.length === 0) {
1010
this.addError(`Title is empty or is not defined value:${yaml.title}`);
@@ -14,7 +14,7 @@ export class TitleHandler extends AbstractYamlHandler {
1414

1515
// Check next handler
1616
if (this.nextHandler) {
17-
return this.nextHandler.handle(yaml,app);
17+
return this.nextHandler.handle(yaml);
1818
}
1919
return this.listOfErrors;
2020
}

src/parsers/embedYamlHandlers/TypeHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { App } from "obsidian";
1111

1212
export class TypeHandler extends AbstractYamlHandler {
1313
handlerName: string = 'type';
14-
public handle(yaml: any, app: App): [string, string][] {
14+
public handle(yaml: any): [string, string][] {
1515

1616
if (!yaml.type) {
1717
// Default type is LIST if not specified
@@ -26,7 +26,7 @@ export class TypeHandler extends AbstractYamlHandler {
2626

2727
// Check next handler
2828
if (this.nextHandler) {
29-
return this.nextHandler.handle(yaml,app);
29+
return this.nextHandler.handle(yaml);
3030
}
3131
return this.listOfErrors;
3232
}

0 commit comments

Comments
 (0)