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

Commit ea303cf

Browse files
committed
parsing objects with yaml
1 parent 9b92eb5 commit ea303cf

File tree

8 files changed

+30
-36
lines changed

8 files changed

+30
-36
lines changed

src/components/cellTypes/Editor/MarkdownEditor.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, Ref, useEffect } from "react";
1+
import React, { forwardRef, MutableRefObject, useEffect } from "react";
22
import { useAutocompleteInputProps } from "components/cellTypes/Editor/autocomplete";
33
import {
44
autoPairBracketsCommands,
@@ -10,22 +10,17 @@ import {
1010
} from "components/cellTypes/Editor/commands";
1111
import { EMITTERS_GROUPS } from "helpers/Constants";
1212
import { MarkdownEditorProps } from "cdm/EditorModel";
13+
import useAutosizeTextArea from "components/styles/hooks/useAutosizeTextArea";
1314

1415
export const MarkdownEditor = forwardRef(function MarkdownEditor(
1516
{ onEnter, onEscape, view, ...inputProps }: MarkdownEditorProps,
16-
ref: Ref<HTMLTextAreaElement>
17+
ref: MutableRefObject<HTMLTextAreaElement>
1718
) {
18-
const shouldAutoPairMarkdown = (app.vault as any).getConfig(
19-
"autoPairMarkdown"
20-
);
21-
const shouldAutoPairBrackets = (app.vault as any).getConfig(
22-
"autoPairBrackets"
23-
);
24-
const shouldUseTab = (app.vault as any).getConfig("useTab");
25-
const tabWidth = (app.vault as any).getConfig("tabSize");
26-
const shouldUseMarkdownLinks = !!(app.vault as any).getConfig(
27-
"useMarkdownLinks"
28-
);
19+
const shouldAutoPairMarkdown = app.vault.getConfig("autoPairMarkdown");
20+
const shouldAutoPairBrackets = app.vault.getConfig("autoPairBrackets");
21+
const shouldUseTab = app.vault.getConfig("useTab");
22+
const tabWidth = app.vault.getConfig("tabSize");
23+
const shouldUseMarkdownLinks = !!app.vault.getConfig("useMarkdownLinks");
2924

3025
const autocompleteProps = useAutocompleteInputProps({
3126
isInputVisible: true,
@@ -97,19 +92,17 @@ export const MarkdownEditor = forwardRef(function MarkdownEditor(
9792
};
9893
}, [view]);
9994

95+
useAutosizeTextArea(ref.current, inputProps.value.toString());
96+
10097
return (
10198
<textarea
10299
{...inputProps}
103100
{...autocompleteProps}
104101
ref={(c: HTMLTextAreaElement) => {
105102
autocompleteProps.ref.current = c;
106-
107-
if (ref && typeof ref === "function") {
108-
ref(c);
109-
} else if (ref) {
110-
(ref as any).current = c;
111-
}
103+
ref.current = c;
112104
}}
105+
rows={inputProps.value.toString()?.split("\n").length || 1}
113106
/>
114107
);
115108
});

src/components/cellTypes/Editor/autocomplete.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ export function constructAutocomplete({
4444
keys: ["file.basename", "alias"],
4545
});
4646

47-
const willAutoPairBrackets = (view.app.vault as any).getConfig(
48-
"autoPairBrackets"
49-
);
47+
const willAutoPairBrackets = view.app.vault.getConfig("autoPairBrackets");
5048

5149
const configs: StrategyProps[] = [
5250
getTagSearchConfig(tags, tagSearch),

src/components/cellTypes/Editor/filepicker.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ export function getFileSearchConfig(
133133
);
134134
}
135135

136-
const shouldUseMarkdownLinks = !!(
137-
app.vault as any
138-
).getConfig('useMarkdownLinks');
136+
const shouldUseMarkdownLinks = !!app.vault.getConfig('useMarkdownLinks');
139137

140138
if (willAutoPairBrackets && !shouldUseMarkdownLinks) {
141139
output[output.length - 1] = output[output.length - 1].slice(0, -2);
@@ -233,9 +231,7 @@ export function getHeadingSearchConfig(
233231
)
234232
);
235233

236-
const shouldUseMarkdownLinks = !!(
237-
app.vault as any
238-
).getConfig('useMarkdownLinks');
234+
const shouldUseMarkdownLinks = !!app.vault.getConfig('useMarkdownLinks');
239235

240236
if (willAutoPairBrackets && !shouldUseMarkdownLinks) {
241237
output[output.length - 1] = output[output.length - 1].slice(0, -2);
@@ -435,9 +431,7 @@ export function getBlockSearchConfig(
435431
)
436432
);
437433

438-
const shouldUseMarkdownLinks = !!(
439-
app.vault as any
440-
).getConfig('useMarkdownLinks');
434+
const shouldUseMarkdownLinks = !!app.vault.getConfig('useMarkdownLinks');
441435

442436
if (willAutoPairBrackets && !shouldUseMarkdownLinks) {
443437
output[output.length - 1] = output[output.length - 1].slice(0, -2);

src/components/cellTypes/EditorCell.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, { ChangeEventHandler, useCallback, useRef } from "react";
44
import { useState } from "react";
55
import { MarkdownEditor } from "components/cellTypes/Editor/MarkdownEditor";
66
import { c } from "helpers/StylesHelper";
7-
import useAutosizeTextArea from "components/styles/hooks/useAutosizeTextArea";
87

98
const EditorCell = (props: EditorCellComponentProps) => {
109
const { defaultCell, persistChange, textCell } = props;
@@ -49,8 +48,6 @@ const EditorCell = (props: EditorCellComponentProps) => {
4948
const handleOnBlur = () => {
5049
persistChange(editorValue?.toString());
5150
};
52-
53-
useAutosizeTextArea(editableMdRef.current, editorValue);
5451
return (
5552
<>
5653
<MarkdownEditor

src/helpers/Constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ export const SUGGESTER_REGEX = Object.freeze({
474474
LINK_BLOCK: /\B\[\[([^#\]]+)#?\^([^\]]*)$/,
475475
EMBED_BLOCK: /\B!\[\[([^#\]]+)#?\^([^\]]*)$/,
476476

477-
TEXT_ARRAY: /(^\[{1})([^\[]{1}.*)(\]{1})$/g
477+
TEXT_ARRAY: /(^\[{1})([^\[]{1}.*)(\]{1})$/g,
478+
TEXT_OBJECT: /(^\{{1})(.*)(\}{1})$/g,
478479
});
479480

480481
/******************************************************************************

src/services/MarkdownRenderService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getNormalizedPath } from "helpers/VaultManagement";
77
import { CachedMetadata, MarkdownRenderer, setIcon, TFile } from "obsidian";
88
import { Literal } from "obsidian-dataview";
99
import { LOGGER } from "services/Logger";
10+
import * as YAML from 'yaml';
1011

1112
const ILLIGAL_CHARS = /[!"#$%&()*+,.:;<=>?@^`{|}~/[\]\\]/g;
1213
class MarkdownRenderService {

src/services/parseServiceHelpers/TextParser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DateTime } from "luxon";
44
import { parseLuxonDatetimeToString } from "helpers/LuxonHelper";
55
import { TypeParser } from "cdm/ServicesModel";
66
import stringifyReplacer from "./StringifyReplacer";
7+
import * as YAML from 'yaml';
78

89
class TextParser extends TypeParser<string | DataObject> {
910
/**
@@ -25,7 +26,8 @@ class TextParser extends TypeParser<string | DataObject> {
2526
}
2627
try {
2728
// Try to parse to JSON
28-
return JSON.stringify(wrapped.value, stringifyReplacer);
29+
const jsonReplaced = JSON.stringify(wrapped.value, stringifyReplacer);
30+
return YAML.stringify(JSON.parse(jsonReplaced));
2931
} catch (e) {
3032
// Do nothing
3133
}

src/typings/obsidian.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ declare module "obsidian" {
1717
*/
1818
processFrontMatter(file: TFile, fn: (frontMatter: Record<string, Literal>) => void): Promise<void>
1919
}
20+
21+
interface Vault {
22+
/**
23+
* TODO: This is a temporary solution. Obsidian not expose official API to get the config.
24+
* @param param
25+
*/
26+
getConfig(param: string): any;
27+
}
2028
}

0 commit comments

Comments
 (0)