Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

### Added

- `FieldMapping.id` names the stable task identity property. It defaults to `id`; mappings saved without it keep using `id`. `mapTaskFromFrontmatter` reads the ID from the mapped property, `toUserField`, `lookupMappingKey` and `validateFieldMapping` include it, and `DEFAULT_ID_PROPERTY` is exported from `@tasknotes/model/mapping`.
- `buildTaskNotesMdbaseResources` uses the mapped ID property for the type's `implements[].fields.id`, schema property, `on_create` UUID, uniqueness rule and the collection's `id_field`. `resolveTaskNotesModelConfigFromMdbaseType` reads a type's `fields.id` into the mapping, and with a remapped ID a plain `id` property resolves as a custom field.

## 0.3.0 - 2026-11-15

Promotes the package to stable semver, retaining the tasknotes-spec 0.3.0
Expand Down
1 change: 1 addition & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from "./types";

export const DEFAULT_FIELD_MAPPING: FieldMapping = {
id: "id",
title: "title",
status: "status",
priority: "priority",
Expand Down
16 changes: 13 additions & 3 deletions src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ export const VALID_DEPENDENCY_RELTYPES: TaskDependencyRelType[] = [
];

export const DEFAULT_RECURRENCE_ORIGINAL_DATE_PROPERTY = "recurrence_original_date";
export const DEFAULT_ID_PROPERTY = "id";

function withResolvedMapping(mapping: FieldMapping): FieldMapping & { recurrenceOriginalDate: string } {
type ResolvedFieldMapping = FieldMapping & { id: string; recurrenceOriginalDate: string };

function withResolvedMapping(mapping: FieldMapping): ResolvedFieldMapping {
return {
...mapping,
id: toUserField(mapping, "id"),
recurrenceOriginalDate: toUserField(mapping, "recurrenceOriginalDate"),
};
}
Expand All @@ -45,6 +49,11 @@ export function normalizeRecurrenceOriginalDate(value: unknown): string | undefi
}

export function toUserField(mapping: FieldMapping, internalName: FieldMappingKey): string {
if (internalName === "id") {
return typeof mapping.id === "string" && mapping.id.trim().length > 0
? mapping.id
: DEFAULT_ID_PROPERTY;
}
if (internalName === "recurrenceOriginalDate") {
return typeof mapping.recurrenceOriginalDate === "string"
? mapping.recurrenceOriginalDate
Expand Down Expand Up @@ -120,8 +129,9 @@ export function mapTaskFromFrontmatter(
const mapped: Partial<TaskInfo> = {
path: filePath,
};
if (typeof frontmatter.id === "string" && frontmatter.id.trim().length > 0) {
mapped.id = frontmatter.id;
const id = frontmatter[toUserField(mapping, "id")];
if (typeof id === "string" && id.trim().length > 0) {
mapped.id = id;
}

if (frontmatter[mapping.title] !== undefined) {
Expand Down
14 changes: 10 additions & 4 deletions src/mdbase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import YAML from "yaml";
import tasknotesTypePack from "./generated/tasknotes-type-pack.json";
import { resolveModelConfig } from "./config";
import { toUserField } from "./mapping";
import {
TASKNOTES_TASK_BINDING_SCHEMA,
TASKNOTES_TASK_COMPLETED_EVENT_SCHEMA,
Expand Down Expand Up @@ -550,6 +551,7 @@ export function buildTaskNotesMdbaseResources(
options.capabilities ?? DEFAULT_TASKNOTES_MDBASE_CAPABILITIES
);
const legacyCompatibility = options.legacyCompatibility === true;
const idField = toUserField(modelConfig.fieldMapping, "id");
const statusValues = uniqueStrings(modelConfig.statuses.map((status) => status.value));
const priorityValues = uniqueStrings(modelConfig.priorities.map((priority) => priority.value));
const completedValues = uniqueStrings(
Expand All @@ -576,7 +578,7 @@ export function buildTaskNotesMdbaseResources(
record_extensions: ["md"],
validation: options.collection?.validation ?? "warn",
explicit_type_keys: ["type", "types"],
id_field: "id",
id_field: idField,
...(excludes.length > 0 ? { exclude: excludes } : {}),
},
};
Expand Down Expand Up @@ -640,7 +642,7 @@ export function buildTaskNotesMdbaseResources(
};

if (options.stableId !== false) {
addField("id", "id", stringSchema({ minLength: 1 }, legacyCompatibility), {
addField("id", idField, stringSchema({ minLength: 1 }, legacyCompatibility), {
createValue: { uuid: true },
});
}
Expand Down Expand Up @@ -812,7 +814,7 @@ export function buildTaskNotesMdbaseResources(
omittedCollectionPaths.add(titleField);
}
if (options.stableId !== false) {
collection.unique = [{ field: "id", scope: "type" }];
collection.unique = [{ field: idField, scope: "type" }];
}

const filenameFormat =
Expand Down Expand Up @@ -1529,7 +1531,11 @@ function resolveUserFields(
fieldMapping: FieldMapping,
fallback: UserMappedField[]
): UserMappedField[] {
const reserved = new Set([...Object.values(fieldMapping), "id", "tags"]);
const reserved = new Set([
...Object.values(fieldMapping),
toUserField(fieldMapping, "id"),
"tags",
]);
const resolved: UserMappedField[] = [];

for (const [key, schema] of Object.entries(schemaProperties)) {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export type TaskUpdateInput = Partial<TaskInfo> & {
};

export interface FieldMapping {
/**
* The stable task identity property. Optional for mappings saved before
* this field existed; those use `id`.
*/
id?: string;
title: string;
status: string;
priority: string;
Expand Down
64 changes: 64 additions & 0 deletions test/id-mapping.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import assert from "node:assert/strict";
import test from "node:test";
import { DEFAULT_FIELD_MAPPING } from "../dist/esm/defaults.js";
import {
lookupMappingKey,
mapTaskFromFrontmatter,
toUserField,
validateFieldMapping,
} from "../dist/esm/mapping.js";
import {
buildTaskNotesMdbaseResources,
resolveTaskNotesModelConfigFromMdbaseType,
} from "../dist/esm/mdbase.js";

const mapping = { ...DEFAULT_FIELD_MAPPING, id: "tasknote_id" };

test("the stable ID is read from its mapped property", () => {
assert.equal(DEFAULT_FIELD_MAPPING.id, "id");
const task = mapTaskFromFrontmatter(
mapping,
{ tasknote_id: "abc", id: "someone-elses", title: "Task" },
"tasks/Task.md"
);
assert.equal(task.id, "abc");
assert.equal(lookupMappingKey(mapping, "tasknote_id"), "id");
assert.equal(lookupMappingKey(mapping, "id"), null);
assert.equal(mapTaskFromFrontmatter(mapping, { id: "plain" }, "tasks/T.md").id, undefined);
assert.equal(validateFieldMapping({ ...mapping, title: "tasknote_id" }).valid, false);
});

test("mappings saved before the ID field existed keep using id", () => {
const legacy = { ...DEFAULT_FIELD_MAPPING };
delete legacy.id;
assert.equal(toUserField(legacy, "id"), "id");
assert.equal(mapTaskFromFrontmatter(legacy, { id: "abc" }, "tasks/T.md").id, "abc");
assert.equal(validateFieldMapping(legacy).valid, true);
});

test("a remapped ID is generated into the mdbase type and read back from it", () => {
const resources = buildTaskNotesMdbaseResources({ modelConfig: { fieldMapping: mapping } });
const { type, config } = resources;
const taskNotes = type.implements.find((entry) => entry.contract === "tasknotes.task");

assert.equal(taskNotes.fields.id, "tasknote_id");
assert.ok(type.schema.value.properties.tasknote_id);
assert.equal(type.schema.value.properties.id, undefined);
assert.deepEqual(type.lifecycle.on_create.set.tasknote_id, { uuid: true });
assert.equal(type.lifecycle.on_create.set.id, undefined);
assert.deepEqual(type.collection.unique, [{ field: "tasknote_id", scope: "type" }]);
assert.equal(config.settings.id_field, "tasknote_id");

const resolved = resolveTaskNotesModelConfigFromMdbaseType(type);
assert.equal(resolved.fieldMapping.id, "tasknote_id");
assert.equal(resolveTaskNotesModelConfigFromMdbaseType(
buildTaskNotesMdbaseResources().type
).fieldMapping.id, "id");
});

test("with a remapped ID, a plain id property is the vault's own field", () => {
const { type } = buildTaskNotesMdbaseResources({ modelConfig: { fieldMapping: mapping } });
type.schema.value.properties.id = { type: "string" };
const resolved = resolveTaskNotesModelConfigFromMdbaseType(type);
assert.deepEqual(resolved.userFields.map((field) => field.key), ["id"]);
});
Loading