Skip to content

Commit

Permalink
chore: remove needless logic for finding headings in mdast
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-lednev committed Nov 10, 2024
1 parent 4dc5211 commit 1ee2332
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 45 deletions.
8 changes: 2 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
fromMarkdown,
positionContainsPoint,
sortListsRecursively,
sortListsRecursivelyUnderHeading,
sortListsRecursivelyInMarkdown,
toEditorPos,
toMarkdown,
toMdastPoint,
Expand Down Expand Up @@ -270,11 +270,7 @@ export default class DayPlanner extends Plugin {
applyScopedUpdates(
contents,
this.settings().plannerHeading,
(scoped) =>
sortListsRecursivelyUnderHeading(
scoped,
this.settings().plannerHeading,
),
sortListsRecursivelyInMarkdown,
)
: undefined;

Expand Down
28 changes: 5 additions & 23 deletions src/mdast/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,17 @@ export function toMarkdown(nodes: Nodes) {
);
}

export function sortListsRecursivelyUnderHeading(
contents: string,
heading: string,
) {
const mdastRoot = fromMarkdown(contents);
const headingWithChildren = findHeadingWithChildren(mdastRoot, heading);

if (!headingWithChildren) {
return contents;
}

const firstNode = headingWithChildren.children.at(0);
const lastNode = headingWithChildren.children.at(-1);

isNotVoid(firstNode?.position?.start?.offset);
isNotVoid(lastNode?.position?.end?.offset);
export function sortListsRecursivelyInMarkdown(contents: string) {
const root = fromMarkdown(contents);

const sorted = {
...headingWithChildren,
children: headingWithChildren.children.map((child) =>
...root,
children: root.children.map((child) =>
sortListsRecursively(child, compareByTimestampInText),
),
};

return (
contents.substring(0, firstNode.position.start.offset) +
toMarkdown(sorted) +
contents.substring(lastNode.position.end.offset + 1)
);
return toMarkdown(sorted);
}

export function findHeadingWithChildren(
Expand Down
3 changes: 3 additions & 0 deletions src/service/diff-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ export function getTaskDiffFromEditState(base: LocalTask[], next: LocalTask[]) {
);
}

/**
* Turns the changes to a view into a list of updates that can be applied to the vault.
*/
export function mapTaskDiffToUpdates(
diff: ViewDiff,
mode: EditMode,
Expand Down
43 changes: 27 additions & 16 deletions tests/diff-writer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { vi, test, expect, describe } from "vitest";

import { defaultDayFormat } from "../src/constants";
import { sortListsRecursivelyUnderHeading } from "../src/mdast/mdast";
import { sortListsRecursivelyInMarkdown } from "../src/mdast/mdast";
import {
applyScopedUpdates,
createTransaction,
Expand Down Expand Up @@ -41,8 +41,9 @@ async function writeDiff(props: {
diff: ViewDiff;
files: Array<InMemoryFile>;
mode: EditMode;
afterEach?: (contents: string) => string;
}) {
const { diff, files, mode } = props;
const { diff, files, mode, afterEach } = props;

const getTasksApi = () => {
throw new Error("Can't access tasks API inside tests");
Expand All @@ -55,17 +56,7 @@ async function writeDiff(props: {
const transaction = createTransaction({
updates,
settings: defaultSettingsForTests,
afterEach: (contents: string) =>
applyScopedUpdates(
contents,
defaultSettingsForTests.plannerHeading,
(scoped) =>
sortListsRecursivelyUnderHeading(
scoped,
// todo: remove heading
defaultSettingsForTests.plannerHeading,
),
),
afterEach,
});
const writer = new TransactionWriter(vaultFacade);

Expand Down Expand Up @@ -281,8 +272,8 @@ describe("From diff to vault", () => {
expect(vault.getAbstractFileByPath(tomorrowDailynotePath).contents)
.toBe(`# Day planner
- 00:00 - 00:30 Moved
- Other
- 00:00 - 00:30 Moved
`);
});

Expand Down Expand Up @@ -492,7 +483,17 @@ describe("From diff to vault", () => {
],
};

const { vault } = await writeDiff({ diff, files, mode: EditMode.DRAG });
const { vault } = await writeDiff({
diff,
files,
mode: EditMode.DRAG,
afterEach: (contents: string) =>
applyScopedUpdates(
contents,
defaultSettingsForTests.plannerHeading,
sortListsRecursivelyInMarkdown,
),
});

expect(vault.getAbstractFileByPath("2023-01-01.md").contents)
.toBe(`# Day planner
Expand Down Expand Up @@ -545,7 +546,17 @@ describe("From diff to vault", () => {
],
};

const { vault } = await writeDiff({ diff, files, mode: EditMode.DRAG });
const { vault } = await writeDiff({
diff,
files,
mode: EditMode.DRAG,
afterEach: (contents: string) =>
applyScopedUpdates(
contents,
defaultSettingsForTests.plannerHeading,
sortListsRecursivelyInMarkdown,
),
});

expect(vault.getAbstractFileByPath("2023-01-01.md").contents)
.toBe(`# Day planner
Expand Down

0 comments on commit 1ee2332

Please sign in to comment.