63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import type { VaultEntry } from '../types'
|
|
import { parseFrontmatter } from '../utils/frontmatter'
|
|
import { frontmatterToEntryPatch } from './frontmatterOps'
|
|
|
|
function createRawEditorEntryState(): Partial<VaultEntry> {
|
|
return {
|
|
aliases: [],
|
|
archived: false,
|
|
belongsTo: [],
|
|
color: null,
|
|
favorite: false,
|
|
favoriteIndex: null,
|
|
icon: null,
|
|
isA: null,
|
|
listPropertiesDisplay: [],
|
|
order: null,
|
|
organized: false,
|
|
properties: {},
|
|
relatedTo: [],
|
|
relationships: {},
|
|
sidebarLabel: null,
|
|
sort: null,
|
|
status: null,
|
|
template: null,
|
|
view: null,
|
|
visible: null,
|
|
}
|
|
}
|
|
|
|
function mergeRelationships(target: Record<string, string[]>, source: Record<string, string[] | null> | null): void {
|
|
if (!source) return
|
|
for (const [key, value] of Object.entries(source)) {
|
|
if (Array.isArray(value) && value.length > 0) target[key] = value
|
|
}
|
|
}
|
|
|
|
function mergeProperties(
|
|
target: Record<string, string | number | boolean | null>,
|
|
source: Record<string, string | number | boolean | null> | null,
|
|
): void {
|
|
if (!source) return
|
|
for (const [key, value] of Object.entries(source)) {
|
|
if (value !== null) target[key] = value
|
|
}
|
|
}
|
|
|
|
export function deriveRawEditorEntryState(content: string): Partial<VaultEntry> {
|
|
const derived = createRawEditorEntryState()
|
|
const properties: Record<string, string | number | boolean | null> = {}
|
|
const relationships: Record<string, string[]> = {}
|
|
|
|
for (const [key, value] of Object.entries(parseFrontmatter(content))) {
|
|
const { patch, relationshipPatch, propertiesPatch } = frontmatterToEntryPatch('update', key, value)
|
|
Object.assign(derived, patch)
|
|
mergeRelationships(relationships, relationshipPatch)
|
|
mergeProperties(properties, propertiesPatch)
|
|
}
|
|
|
|
derived.properties = properties
|
|
derived.relationships = relationships
|
|
return derived
|
|
}
|