Skip to content

Commit

Permalink
feat: add defaults for frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
waynevanson committed Aug 28, 2023
1 parent 9d3f573 commit 99f26b7
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 36 deletions.
22 changes: 16 additions & 6 deletions .vault/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
"type": "split",
"children": [
{
"id": "0716682fc1a1b562",
"id": "1f83ec277e87be99",
"type": "tabs",
"children": [
{
"id": "d5403f62d610686b",
"id": "d89bcd3b4839c10b",
"type": "leaf",
"state": {
"type": "empty",
"state": {}
"type": "markdown",
"state": {
"file": "Sample 05.md",
"mode": "preview",
"source": true
}
}
}
]
Expand Down Expand Up @@ -81,6 +85,7 @@
"state": {
"type": "backlink",
"state": {
"file": "Sample 05.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -97,6 +102,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "Sample 05.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -118,7 +124,9 @@
"type": "leaf",
"state": {
"type": "outline",
"state": {}
"state": {
"file": "Sample 05.md"
}
}
}
],
Expand All @@ -139,8 +147,10 @@
"command-palette:Open command palette": false
}
},
"active": "d5403f62d610686b",
"active": "d89bcd3b4839c10b",
"lastOpenFiles": [
"Sample 04.md",
"Sample 05.md",
"Sample 03.md",
"Sample 02.md",
"data.md",
Expand Down
2 changes: 1 addition & 1 deletion .vault/Sample 03.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ schema:
name:
type: string
data:
name: Yellow Black
name: Shrek the best
---

```yaml-data-entry
Expand Down
14 changes: 14 additions & 0 deletions .vault/Sample 04.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
schema:
properties:
name:
type: string
data: {}
---

```yaml-data-entry
datasource:
file:
schema:
file:
```
14 changes: 14 additions & 0 deletions .vault/Sample 05.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
schema:
properties:
name:
type: string
data: {}
---

```yaml-data-entry
datasource:
file:
schema:
file:
```
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,17 @@ schema:
type: array
items:
type: string
data:
---

```yaml-data-entry
datasource:
file:
path: .
frontmatter: data
schema:
file:
path: .
frontmatter: schema
```



</code></pre>

## Please Note
Expand Down
2 changes: 1 addition & 1 deletion packages/data-entry/src/components/context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App } from 'obsidian';
import { createContext, useContext } from 'react';
import { ApplicationConfiguration } from '../common';
import { ApplicationConfiguration } from '../config';

export interface ApplicationProps {
app: App;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { option, readonlyRecord, string } from 'fp-ts';
import { pipe } from 'fp-ts/lib/function';
import { decoder } from './lib';
import { Decoder } from 'io-ts/Decoder';
import { Sum } from './lib/sum';

export type Datasource = Sum<{
file: {
path: string;
frontmatter: string; // defaults to 'data'
};
}>;

export const configuration = (
defaults: Record<
'datasource' | 'uischema' | 'schema',
Record<'path', string>
Record<'path' | 'frontmatter', string>
>,
) =>
pipe(
Expand All @@ -37,17 +27,12 @@ export const configuration = (

const path = pipe(
decoder.string,
// allows relative paths
// todo - allows relative paths
decoder.map((string) => string.replace(/^\./, '')),
);

const file = (defaults: Record<'path', string>) =>
pipe(
decoder.struct({
frontmatter: decoder.string,
}),
decoder.intersect(decoder.configurable({ path }, defaults)),
);
const file = (defaults: Record<'path' | 'frontmatter', string>) =>
decoder.configurable({ path, frontmatter: decoder.string }, defaults);

export type ApplicationConfiguration = decoder.TypeOf<
ReturnType<typeof configuration>
Expand Down
1 change: 1 addition & 0 deletions packages/data-entry/src/lib/decoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ export const configurable = <A extends Record<string, unknown>>(
readonlyRecord.filter((a): a is NonNullable<typeof a> => a != null),
),
),
decoder.nullable,
decoder.map((partials) => ({ ...defaults, ...partials })),
);
8 changes: 4 additions & 4 deletions packages/data-entry/src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import * as React from 'react';
import { StrictMode } from 'react';
import { Root, createRoot } from 'react-dom/client';
import { configuration } from './common';
import { configuration } from './config';
import { Application } from './components';
import { ApplicationProvider } from './components/context';
import { useTheme } from './components/material';
Expand Down Expand Up @@ -72,9 +72,9 @@ export class MainPlugin extends Plugin {
either.chainW(
flow(
configuration({
datasource: { path },
schema: { path },
uischema: { path },
datasource: { path, frontmatter: 'data' }, // todo - change to `datasource`
schema: { path, frontmatter: 'schema' },
uischema: { path, frontmatter: 'uischema' },
}).decode,
either.mapLeft(decoder.draw),
),
Expand Down

0 comments on commit 99f26b7

Please sign in to comment.