Skip to content

Commit

Permalink
fix(publish): skip adding asset prefix to images with web url (#2362)
Browse files Browse the repository at this point in the history
* fix(publish): add .next to gitignore on dendron publish init

* fix(publish): skip adding assetPrefix for remote images

* fix: resolve PR comment
  • Loading branch information
Harshita-mindfire committed Feb 7, 2022
1 parent c42f32d commit 11cf84c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/dendron-cli/src/commands/publishCLICommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { SetupEngineCLIOpts } from "./utils";
import prompts from "prompts";
import fs from "fs-extra";
import ora from "ora";
import { GitUtils } from "@dendronhq/common-server";

type CommandCLIOpts = {
cmd: PublishCommands;
Expand Down Expand Up @@ -311,6 +312,7 @@ export class PublishCLICommand extends CLICommand<CommandOpts, CommandOutput> {

async init(opts: { wsRoot: string; spinner: ora.Ora }) {
const { wsRoot, spinner } = opts;
GitUtils.addToGitignore({ addPath: ".next", root: wsRoot });
const nextPath = NextjsExportPodUtils.getNextRoot(wsRoot);

const nextPathExists = await this._nextPathExists({
Expand Down
8 changes: 6 additions & 2 deletions packages/engine-server/src/markdown/remark/dendronPub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
DendronError,
ERROR_SEVERITY,
isNotUndefined,
isWebUri,
NoteProps,
NoteUtils,
StatusCodes,
Expand Down Expand Up @@ -116,8 +117,11 @@ class ImageNodeHandler extends DendronNodeHander {
: cOpts?.assetsPrefix;
const imageNode = node;
if (assetsPrefix) {
imageNode.url =
"/" + _.trim(assetsPrefix, "/") + "/" + _.trim(imageNode.url, "/");
const imageUrl = _.trim(imageNode.url, "/");
// do not add assetPrefix for http/https url
imageNode.url = !isWebUri(imageUrl)
? "/" + _.trim(assetsPrefix, "/") + "/" + imageUrl
: imageUrl;
}
return { node: imageNode };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GIVEN image link WHEN assetPrefix is set and and image url is not local "HTML: REMOTE_IMAGE: PUBLISHING" 1`] = `
VFile {
"contents": "<h1 id=\\"foo\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#foo\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Foo</h1>
<p><img src=\\"https://foundation-prod-assetspublic53c57cce-8cpvgjldwysl.s3-us-west-2.amazonaws.com/assets/images/not-sprouted.png\\" alt=\\"second-image\\"></p>
<hr>
<strong>Children</strong>
<ol>
<li><a href=\\"/some-prefix/notes/foo.ch1.html\\">Ch1</a></li>
</ol>",
"cwd": "<PROJECT_ROOT>",
"data": Object {},
"history": Array [],
"messages": Array [],
}
`;
exports[`GIVEN image link WHEN assetPrefix set "HTML: ASSET_PREFIX_SET: PUBLISHING" 1`] = `
VFile {
"contents": "<h1 id=\\"foo\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#foo\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Foo</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,39 @@ describe("GIVEN image link", () => {
})
);
});
describe("WHEN assetPrefix is set and and image url is not local ", () => {
runTestCases(
createProcCompileTests({
name: "REMOTE_IMAGE",
setup: async (opts) => {
const { proc } = getOpts(opts);
const txt = `![second-image](https://foundation-prod-assetspublic53c57cce-8cpvgjldwysl.s3-us-west-2.amazonaws.com/assets/images/not-sprouted.png)`;
const resp = await proc.process(txt);
return { resp, proc };
},
verify: {
[DendronASTDest.HTML]: {
[ProcFlavor.PUBLISHING]: async ({ extra }) => {
const { resp } = extra;
expect(resp).toMatchSnapshot();
await checkString(
resp.contents,
"https://foundation-prod-assetspublic53c57cce-8cpvgjldwysl.s3-us-west-2.amazonaws.com/assets/images/not-sprouted.png"
);
},
},
},
preSetupHook: async (opts) => {
await ENGINE_HOOKS.setupBasic({ ...opts, extra: { idv2: true } });
TestConfigUtils.withConfig(
(config) => {
config.site.assetsPrefix = "/some-prefix";
return config;
},
{ wsRoot: opts.wsRoot }
);
},
})
);
});
});

0 comments on commit 11cf84c

Please sign in to comment.