Skip to content

Commit

Permalink
fix: update links on frontmatter tags changes (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaan Genç committed Aug 26, 2021
1 parent d6516d1 commit 4d344fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/plugin-core/src/services/NoteSyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@ export class NoteSyncService {
});
note = NoteUtils.hydrate({ noteRaw: note, noteHydrated });

// Links have to be updated even with frontmatter only changes
// because `tags` in frontmatter adds new links
const links = LinkUtils.findLinks({ note, engine: eclient });
note.links = links;

// iif frontmatter changed, don't bother with heavy updates
if (!fmChangeOnly) {
const links = LinkUtils.findLinks({ note, engine: eclient });
const notesMap = NoteUtils.createFnameNoteMap(
_.values(eclient.notes),
true
);
note.links = links;
const anchors = await AnchorUtils.findAnchors({
note,
wsRoot: eclient.wsRoot,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Time } from "@dendronhq/common-all";
import { AssertUtils } from "@dendronhq/common-test-utils";
import { AssertUtils, NoteTestUtilsV4 } from "@dendronhq/common-test-utils";
import { ENGINE_HOOKS_MULTI } from "@dendronhq/engine-test-utils";
import _ from "lodash";
import { DateTime } from "luxon";
Expand Down Expand Up @@ -56,6 +56,36 @@ suite("NoteSyncService", function testSuite() {
});
});

test("onDidChange: changing `tags` updates links", (done) => {
runLegacyMultiWorkspaceTest({
ctx,
postSetupHook: async (opts) => {
await ENGINE_HOOKS_MULTI.setupBasicMulti(opts);
await NoteTestUtilsV4.createNote({
fname: "tags.test",
wsRoot: opts.wsRoot,
vault: opts.vaults[0],
});
},
onInit: async ({ engine }) => {
const foo = engine.notes["foo"];
const editor = await VSCodeUtils.openNote(foo);
await editor?.edit((builder) => {
const pos = new vscode.Position(6, 0);
builder.insert(pos, `tags: test\n`);
});
await NoteSyncService.instance().onDidChange(editor);

// "foo" should have the frontmatter link to "tags.test"
const updatedFoo = engine.notes["foo"];
expect(updatedFoo.links.length).toEqual(1);
expect(updatedFoo.links[0].type).toEqual("frontmatterTag");
expect(updatedFoo.links[0].to?.fname).toEqual("tags.test");
done();
},
});
});

test("onDidChange: no change", (done) => {
runLegacyMultiWorkspaceTest({
ctx,
Expand Down

0 comments on commit 4d344fe

Please sign in to comment.