Skip to content

Commit

Permalink
fix: Backlinks will no longer disappear in preview upon editing
Browse files Browse the repository at this point in the history
  • Loading branch information
tma66 committed Mar 23, 2022
1 parent a145bc2 commit 8a99bfd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
8 changes: 5 additions & 3 deletions packages/common-all/src/dnode.ts
Expand Up @@ -1095,9 +1095,11 @@ export class NoteUtils {
noteHydrated: NoteProps;
}) {
const hydrateProps = _.pick(noteHydrated, ["parent", "children"]);
noteRaw.links = noteHydrated.links.filter(
(link) => link.type === "backlink"
);
if (noteHydrated) {
noteRaw.links = noteHydrated.links.filter(
(link) => link.type === "backlink"
);
}
return { ...noteRaw, ...hydrateProps };
}

Expand Down
3 changes: 2 additions & 1 deletion packages/engine-server/src/drivers/file/storev2.ts
Expand Up @@ -465,7 +465,8 @@ export class FileStorage implements DStore {
try {
noteFrom.links.forEach((link) => {
const fname = link.to?.fname;
if (fname) {
// Note referencing itself does not count as backlink
if (fname && fname !== noteFrom.fname) {
const notes = noteCache.getNotesByFileNameIgnoreCase(fname);

notes.forEach((noteTo: NoteProps) => {
Expand Down
Expand Up @@ -70,6 +70,33 @@ Array [
]
`;

exports[`RemarkUtils and LinkUtils findLinks backlink 1`] = `
Array [
Object {
"alias": undefined,
"from": Object {
"fname": "simple-note-ref",
"vaultName": "vault1",
},
"position": Position {
"end": Object {
"column": 25,
"line": 1,
"offset": 24,
},
"indent": Array [],
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "backlink",
"value": "simple-note-ref.one",
},
]
`;

exports[`RemarkUtils and LinkUtils findLinks empty link 1`] = `Array []`;

exports[`RemarkUtils and LinkUtils findLinks filter loc match 1`] = `
Expand Down Expand Up @@ -204,6 +231,28 @@ Array [
"value": "foo.two",
"xvault": false,
},
Object {
"alias": undefined,
"from": Object {
"fname": "foo",
"vaultName": "vault1",
},
"position": Position {
"end": Object {
"column": 13,
"line": 1,
"offset": 12,
},
"indent": Array [],
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "backlink",
"value": "foo.one",
},
]
`;

Expand Down
Expand Up @@ -220,23 +220,17 @@ describe("RemarkUtils and LinkUtils", () => {
}
);

// TODO: Add new test case
testWithEngine(
"backlink",
async ({ engine }) => {
const note = engine.notes["foo"];
const note = engine.notes["simple-note-ref.one"];
const links = LinkUtils.findLinks({ note, engine });
expect(links).toMatchSnapshot();
expect(links[0].to?.fname).toEqual("bar");
expect(links[0].from?.fname).toEqual("simple-note-ref");
},
{
preSetupHook: async ({ wsRoot, vaults }) => {
await NoteTestUtilsV4.createNote({
fname: "foo",
body: "[[bar]]",
vault: vaults[0],
wsRoot,
});
preSetupHook: async (opts) => {
await ENGINE_HOOKS.setupRefs(opts);
},
}
);
Expand Down Expand Up @@ -317,6 +311,16 @@ describe("RemarkUtils and LinkUtils", () => {
},
target: links[1],
});
checkLink({
src: {
from: {
fname: "foo",
vaultName: "vault1",
},
type: "backlink",
},
target: links[2],
});
},
{
expect,
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-core/src/services/TextDocumentService.ts
Expand Up @@ -156,7 +156,6 @@ export class TextDocumentService implements ITextDocumentService {
});
return noteHydrated;
}

const props = await this.updateNoteContents({
oldNote: noteHydrated,
content,
Expand Down

0 comments on commit 8a99bfd

Please sign in to comment.