Skip to content

Commit

Permalink
fix: exclude private vault backlinks (#1301)
Browse files Browse the repository at this point in the history
* fix: filter out non-publishable backlinks

* chore: add tests for private vault backlinks

* chore: add example test setup to test-workspace

* chore: use canPublish to check filter backlinks instead
  • Loading branch information
hikchoi committed Sep 8, 2021
1 parent bcf0dea commit 837c50e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 4 deletions.
29 changes: 27 additions & 2 deletions packages/engine-server/src/markdown/remark/backlinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NoteUtils, VaultUtils } from "@dendronhq/common-all";
import _ from "lodash";
import { Content, Root } from "mdast";
import { list, listItem, paragraph } from "mdast-builder";
import { SiteUtils } from "../../topics/site";
import Unified, { Plugin } from "unified";
import { Node } from "unist";
import u from "unist-builder";
Expand Down Expand Up @@ -37,7 +38,31 @@ const plugin: Plugin = function (this: Unified.Processor) {
(ent) => ent.from.fname + (ent.from.vaultName || "")
);

if (!_.isEmpty(backlinks)) {
const backlinksToPublish = _.filter(backlinks, (backlink) => {
const vaultName = backlink.from.vaultName!;
const vault = VaultUtils.getVaultByName({
vaults: engine.vaults,
vname: vaultName,
})!;
const note = NoteUtils.getNoteByFnameV5({
fname: backlink.from.fname!,
notes: engine.notes,
vault,
wsRoot: engine.wsRoot,
});

if (!note) {
return false;
}
const out = SiteUtils.canPublish({
note,
engine,
config: engine.config,
});
return out;
});

if (!_.isEmpty(backlinksToPublish)) {
root.children.push({
type: "thematicBreak",
});
Expand All @@ -47,7 +72,7 @@ const plugin: Plugin = function (this: Unified.Processor) {
root.children.push(
list(
"unordered",
backlinks.map((mdLink) => {
backlinksToPublish.map((mdLink) => {
return listItem(
paragraph({
type: DendronASTTypes.WIKI_LINK,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DEngineClient } from "@dendronhq/common-all";
import { DEngineClient, DVaultVisibility } from "@dendronhq/common-all";
import { tmpDir } from "@dendronhq/common-server";
import { AssertUtils, NoteTestUtilsV4 } from "@dendronhq/common-test-utils";
import { ENGINE_HOOKS } from "../../../presets";
import { ENGINE_HOOKS, ENGINE_HOOKS_MULTI } from "../../../presets";
import {
DendronASTData,
DendronASTDest,
Expand Down Expand Up @@ -152,6 +152,76 @@ describe("backlinks", () => {
);
});

test("backlinks to private vaults not added", async () => {
await runEngineTestV5(
async ({ engine, vaults }) => {
const vault = vaults[0];
const resp = await MDUtilsV4.procRehype({
proc: proc(engine, {
fname: "one",
vault,
dest,
config: engine.config,
}),
}).process("");
expect(
await AssertUtils.assertInString({
body: resp.contents as string,
nomatch: [
`<a href="secret1.html">Secret1 (vault2)</a>`,
`<a href="secret2.html">Secret2 (vault2)</a>`
],
match: [
`<a href="not-secret.html">Not Secret (vaultThree)</a>`
]
})
).toBeTruthy();
},
{
expect,
preSetupHook: async (opts: any) => {
await ENGINE_HOOKS_MULTI.setupBasicMulti(opts);
TestConfigUtils.withConfig(
(config) => {
const bvault = config.vaults.find(
(ent: any) => ent.fsPath === "vault2"
);
bvault!.visibility = DVaultVisibility.PRIVATE;
return config;
},
{ wsRoot: opts.wsRoot }
);

const { wsRoot, vaults } = opts;
await NoteTestUtilsV4.createNote({
fname: "one",
vault: vaults[0],
wsRoot,
body: "one"
});
await NoteTestUtilsV4.createNote({
fname: "secret1",
vault: vaults[1],
wsRoot,
body: "[[one]]"
});
await NoteTestUtilsV4.createNote({
fname: "secret2",
vault: vaults[1],
wsRoot,
body: "[[one]]"
});
await NoteTestUtilsV4.createNote({
fname: "not-secret",
vault: vaults[2],
wsRoot,
body: "[[one]]"
})
},
}
);
})

describe("frontmatter tags", () => {
test("single", async () => {
await runEngineTestV5(
Expand Down
9 changes: 9 additions & 0 deletions test-workspace/vault2/secret.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
id: tG7Bj5kiO6UiJBVGipMkG
title: Secret
desc: ''
updated: 1631003961643
created: 1631003961643
---

[[dendron.ref.links]]
9 changes: 9 additions & 0 deletions test-workspace/vault3/not-secret.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
id: Zq8Kz00hOPxCo4HdS5rdt
title: Not Secret
desc: ''
updated: 1631003866830
created: 1631003866830
---

[[dendron.ref.links]]

0 comments on commit 837c50e

Please sign in to comment.