Skip to content

Commit 879c238

Browse files
committed
🐛(obsidian) Address ObsidianReviewBot code scan findings
1 parent af6eafd commit 879c238

6 files changed

Lines changed: 34 additions & 18 deletions

File tree

esbuild.config.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import esbuild from "esbuild";
22
import process from "process";
33
import { builtinModules } from "node:module";
4+
import { createRequire } from "node:module";
5+
6+
const require = createRequire(import.meta.url);
7+
const topologyUiCjsEntry = require.resolve("@couchbaselabs/topology-ui");
48

59
const banner =
610
`/*
@@ -17,6 +21,16 @@ const context = await esbuild.context({
1721
entryPoints: ["src/main.ts"],
1822
bundle: true,
1923
mainFields: ["main", "module"],
24+
plugins: [
25+
{
26+
name: "topology-ui-cjs-entry",
27+
setup(build) {
28+
build.onResolve({ filter: /^@couchbaselabs\/topology-ui$/ }, () => ({
29+
path: topologyUiCjsEntry
30+
}));
31+
}
32+
}
33+
],
2034
external: [
2135
"obsidian",
2236
"electron",

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "couchbase-topology",
33
"name": "Couchbase Topology",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"minAppVersion": "1.12.0",
66
"description": "Render Couchbase topology diagrams from fenced code blocks.",
77
"author": "Couchbase Labs",

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-couchbase-topology",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Obsidian plugin for rendering Couchbase topology fenced code blocks with the topology-ui library.",
55
"main": "dist/main.js",
66
"type": "module",

src/main.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
import { MarkdownPostProcessorContext, Plugin } from "obsidian";
2+
import topologyUi from "@couchbaselabs/topology-ui";
23
import { topologyAssetDataUris } from "./generated/topology-assets";
34

4-
type TopologyUiModule = {
5-
parseTopologySource: (input: string, options?: { allowJavaScript?: boolean }) => unknown;
6-
renderTopology: (input: unknown) => string;
7-
};
8-
9-
const topologyUi = require("@couchbaselabs/topology-ui") as TopologyUiModule;
10-
115
export default class CouchbaseTopologyPlugin extends Plugin {
126
private topologyUi = topologyUi;
137

14-
async onload(): Promise<void> {
15-
this.registerMarkdownCodeBlockProcessor("couchbase-topology", async (source, el, ctx) => {
16-
await this.renderTopologyCodeBlock(source, el, ctx);
8+
onload(): void {
9+
this.registerMarkdownCodeBlockProcessor("couchbase-topology", (source, el, ctx) => {
10+
this.renderTopologyCodeBlock(source, el, ctx);
1711
});
1812
}
1913

20-
private async renderTopologyCodeBlock(
14+
private renderTopologyCodeBlock(
2115
source: string,
2216
el: HTMLElement,
2317
_ctx: MarkdownPostProcessorContext
24-
): Promise<void> {
18+
): void {
2519
const host = el.createDiv({ cls: "couchbase-topology-block" });
2620

2721
try {
2822
const topology = this.topologyUi.parseTopologySource(source);
29-
host.innerHTML = this.inlineTopologyAssets(this.topologyUi.renderTopology(topology));
23+
const renderedMarkup = this.inlineTopologyAssets(this.topologyUi.renderTopology(topology));
24+
host.replaceChildren(this.createRenderedFragment(host, renderedMarkup));
3025
} catch (error) {
3126
const message = error instanceof Error ? error.message : String(error);
3227
host.empty();
@@ -38,6 +33,12 @@ export default class CouchbaseTopologyPlugin extends Plugin {
3833
}
3934
}
4035

36+
private createRenderedFragment(host: HTMLElement, markup: string): DocumentFragment {
37+
const range = host.ownerDocument.createRange();
38+
range.selectNodeContents(host);
39+
return range.createContextualFragment(markup);
40+
}
41+
4142
private inlineTopologyAssets(html: string): string {
4243
return html.replace(
4344
/\b(xlink:href|src)=(["'])images\/([^"'?#]+)\2/g,

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"0.1.0": "1.12.0",
3-
"0.1.1": "1.12.0"
3+
"0.1.1": "1.12.0",
4+
"0.1.2": "1.12.0"
45
}

0 commit comments

Comments
 (0)