Skip to content

Commit

Permalink
Only print debugging info in dev builds
Browse files Browse the repository at this point in the history
  • Loading branch information
iafisher committed Jul 28, 2024
1 parent db2bdbb commit f1c289b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const context = await esbuild.context({
},
entryPoints: ["src/main.ts"],
bundle: true,
define: {
"DEBUG": JSON.stringify(!prod),
},
external: [
"obsidian",
"electron",
Expand Down
39 changes: 30 additions & 9 deletions src/live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ interface QuickLinkSlice {
to: number;
}

// defined by esbuild
declare var DEBUG: boolean;

class LivePreviewQuickLinksPluginValue implements PluginValue {
decorations: DecorationSet;
private slices: QuickLinkSlice[];
Expand Down Expand Up @@ -63,7 +66,9 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
from,
to,
enter: (node: SyntaxNodeRef) => {
console.debug("Found node:", node.node.type.name);
if (DEBUG) {
console.debug("Found node:", node.node.type.name);
}
nodes.push(node.node);
},
});
Expand All @@ -79,15 +84,19 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
"hmd-internal-link",
"formatting-link_formatting-link-end",
];
console.debug("Searching for plain internal links");
if (DEBUG) {
console.debug("Searching for plain internal links");
}
for (const chunk of findChunks(nodes, plainInternalLinkPattern)) {
console.assert(chunk.length === 3);
const from = chunk[0].from;
const to = chunk[chunk.length - 1].to;
const target = view.state.sliceDoc(chunk[1].from, chunk[1].to);

const link = { text: "", target, em: chunk[0].name.startsWith("em") };
console.debug("Found link (plain internal)", link);
if (DEBUG) {
console.debug("Found link (plain internal)", link);
}
this.handleLink(link, false, { from, to }, slices, quickLinksMap);
}

Expand All @@ -99,7 +108,9 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
"hmd-internal-link_link-alias",
"formatting-link_formatting-link-end",
];
console.debug("Searching for piped internal links");
if (DEBUG) {
console.debug("Searching for piped internal links");
}
for (const chunk of findChunks(nodes, pipedInternalLinkPattern)) {
console.assert(chunk.length === 5);
const from = chunk[0].from;
Expand All @@ -108,7 +119,9 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
const text = view.state.sliceDoc(chunk[3].from, chunk[3].to);

const link = { text, target, em: chunk[0].name.startsWith("em") };
console.debug("Found link (piped internal)", link);
if (DEBUG) {
console.debug("Found link (piped internal)", link);
}
this.handleLink(link, false, { from, to }, slices, quickLinksMap);
}
}
Expand All @@ -122,7 +135,9 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
"string_url",
"formatting_formatting-link-string_string_url",
];
console.debug("Searching for external links");
if (DEBUG) {
console.debug("Searching for external links");
}
for (const chunk of findChunks(nodes, externalLinkPattern1)) {
console.assert(chunk.length === 6);
const from = chunk[0].from;
Expand All @@ -131,7 +146,9 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
const text = view.state.sliceDoc(chunk[1].from, chunk[1].to);

const link = { text, target, em: false };
console.debug("Found link (external)", link);
if (DEBUG) {
console.debug("Found link (external)", link);
}
this.handleLink(link, true, { from, to }, slices, quickLinksMap);
}

Expand All @@ -142,7 +159,9 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
"string_url",
"formatting_formatting-link-string_string_url",
];
console.debug("Searching for external links (pattern2)");
if (DEBUG) {
console.debug("Searching for external links (pattern2)");
}
for (const chunk of findChunks(nodes, externalLinkPattern2)) {
console.assert(chunk.length === 4);
const from = chunk[0].from;
Expand All @@ -151,7 +170,9 @@ class LivePreviewQuickLinksPluginValue implements PluginValue {
const text = "";

const link = { text, target, em: false };
console.debug("Found link (external)", link);
if (DEBUG) {
console.debug("Found link (external)", link);
}
this.handleLink(link, true, { from, to }, slices, quickLinksMap);
}
}
Expand Down

0 comments on commit f1c289b

Please sign in to comment.