Skip to content

Commit 163d1d5

Browse files
Seraphliclaude
andcommitted
feat: add delete button to remove embed blocks, fix #72
- Add delete button with trash icon to embed card buttons - Implement addDeleteButtonHandler to remove entire embed block from file - Reorder buttons: delete, refresh, copy (left to right) - Update CSS to include delete button styling - Refactor build process to output directly to build/ directory - Modify esbuild.config.mjs to output to build/main.js - Auto-copy manifest.json and styles.css to build/ - Remove copyBuild command and update publish scripts - Bump version to 2.9.3 Closes #72 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 644404f commit 163d1d5

File tree

8 files changed

+123
-15
lines changed

8 files changed

+123
-15
lines changed

esbuild.config.mjs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import esbuild from "esbuild";
22
import process from "process";
3-
import builtins from 'builtin-modules'
3+
import builtins from 'builtin-modules';
4+
import { copyFileSync, mkdirSync } from 'fs';
45

56
const banner =
67
`/*
@@ -47,11 +48,19 @@ const buildOptions = {
4748
logLevel: "info",
4849
sourcemap: prod ? false : 'inline',
4950
treeShaking: true,
50-
outfile: 'main.js',
51+
outfile: 'build/main.js',
5152
};
5253

5354
if (prod) {
54-
esbuild.build(buildOptions).catch(() => process.exit(1));
55+
// Create build directory if it doesn't exist
56+
mkdirSync('build', { recursive: true });
57+
58+
esbuild.build(buildOptions).then(() => {
59+
// Copy additional files to build directory
60+
copyFileSync('manifest.json', 'build/manifest.json');
61+
copyFileSync('styles.css', 'build/styles.css');
62+
console.log('✓ Build complete - files copied to build/');
63+
}).catch(() => process.exit(1));
5564
} else {
5665
esbuild.context(buildOptions).then(ctx => ctx.watch()).catch(() => process.exit(1));
5766
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-link-embed",
33
"name": "Link Embed",
4-
"version": "2.9.2",
4+
"version": "2.9.3",
55
"minAppVersion": "0.12.0",
66
"description": "This plugin auto-fetches page metadata to embed Notion-style link preview cards.",
77
"author": "SErAphLi",

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-link-embed",
3-
"version": "2.9.2",
3+
"version": "2.9.3",
44
"description": "This plugin auto-fetches page metadata to embed Notion-style link preview cards.",
55
"main": "main.js",
66
"scripts": {
@@ -10,9 +10,7 @@
1010
"lint:fix": "eslint --fix \"src/**/*.{ts,js}\" \"*.{ts,js}\"",
1111
"build": "npm run format && tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
1212
"version": "node version-bump.mjs && git add manifest.json versions.json",
13-
"copyBuild": "make-dir build && rimraf build/main.js build/manifest.json build/styles.css && cpy main.js manifest.json styles.css build/",
14-
"prepublish": "npm run build && npm run copyBuild",
15-
"publish": "npm run build && npm run version && npm run copyBuild",
13+
"publish": "npm run build && npm run version",
1614
"prepare": "husky"
1715
},
1816
"keywords": [],

src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ aspectRatio: "{{aspectRatio}}"{{/aspectRatio}}{{#metadata}}
1111
export const HTMLTemplate = `<div class="embed">
1212
<div class="w _lc _sm _od _lh14 _ts">
1313
<div class="embed-buttons">
14+
<div class="delete-button">
15+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
16+
<polyline points="3 6 5 6 21 6"></polyline>
17+
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
18+
<line x1="10" y1="11" x2="10" y2="17"></line>
19+
<line x1="14" y1="11" x2="14" y2="17"></line>
20+
</svg>
21+
</div>
1422
<div class="refresh-button">
1523
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
1624
<path d="M21.5 2v6h-6M2.5 22v-6h6M2 11.5a10 10 0 0 1 18.8-4.3M22 12.5a10 10 0 0 1-18.8 4.2"/>

src/embedUtils.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,85 @@ export function addCopyButtonHandler(
484484
}
485485
}
486486

487+
/**
488+
* Add a click handler for the delete button that removes the entire embed block from the file.
489+
*
490+
* @param element The HTML element containing the embed
491+
* @param embedInfo Embed information (not used but kept for consistency)
492+
* @param ctx Markdown post processor context
493+
* @param vault The vault instance (needed to modify file content)
494+
* @param settings Plugin settings
495+
*/
496+
export function addDeleteButtonHandler(
497+
element: HTMLElement,
498+
embedInfo: EmbedInfo,
499+
ctx?: MarkdownPostProcessorContext,
500+
vault?: any,
501+
settings?: ObsidianLinkEmbedPluginSettings,
502+
): void {
503+
const deleteButton = element.querySelector('.delete-button');
504+
if (deleteButton) {
505+
deleteButton.addEventListener('click', async () => {
506+
try {
507+
// Get the current file
508+
const file = vault.getAbstractFileByPath(ctx.sourcePath);
509+
if (!file) {
510+
showNotice(`File not found: ${ctx.sourcePath}`, {
511+
debug: settings.debug,
512+
context: 'Link Embed - Delete',
513+
type: 'error',
514+
});
515+
return;
516+
}
517+
518+
// Get section info to locate the code block
519+
const sectionInfo = ctx.getSectionInfo(element);
520+
if (!sectionInfo) {
521+
showNotice('Could not get section info', {
522+
debug: settings.debug,
523+
context: 'Link Embed - Delete',
524+
type: 'error',
525+
});
526+
return;
527+
}
528+
529+
// Get file content and remove the embed block
530+
const content = await vault.read(file);
531+
const lines = content.split('\n');
532+
const startLine = sectionInfo.lineStart;
533+
const endLine = sectionInfo.lineEnd + 1;
534+
535+
// Remove the embed block lines
536+
const newLines = [
537+
...lines.slice(0, startLine),
538+
...lines.slice(endLine),
539+
];
540+
const newContent = newLines.join('\n');
541+
542+
// Write the modified content back to the file
543+
await vault.modify(file, newContent);
544+
545+
showNotice('Embed block deleted', {
546+
debug: settings?.debug || false,
547+
context: 'Link Embed - Delete',
548+
type: 'success',
549+
});
550+
} catch (error) {
551+
showNotice(
552+
error instanceof Error
553+
? error
554+
: `Error deleting embed block: ${String(error)}`,
555+
{
556+
debug: settings?.debug || false,
557+
context: 'Link Embed - Delete',
558+
type: 'error',
559+
},
560+
);
561+
}
562+
});
563+
}
564+
}
565+
487566
/**
488567
* Embed a URL into the editor.
489568
*

src/eventHandlers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
renderEmbed,
1010
addRefreshButtonHandler,
1111
addCopyButtonHandler,
12+
addDeleteButtonHandler,
1213
} from './embedUtils';
1314
import { showNotice } from './errorUtils';
1415
import { getImageDimensions } from './parsers';
@@ -277,6 +278,7 @@ export async function handleEmbedCodeBlock(
277278
// Add handlers to the initial render
278279
addRefreshButtonHandler(newEl, info, ctx, settings, vault);
279280
addCopyButtonHandler(newEl, info, ctx, vault, settings);
281+
addDeleteButtonHandler(newEl, info, ctx, vault, settings);
280282

281283
// If we have any promises, wait for all to complete then do final render
282284
if (promises.length > 0) {
@@ -306,6 +308,13 @@ export async function handleEmbedCodeBlock(
306308
vault,
307309
settings,
308310
);
311+
addDeleteButtonHandler(
312+
finalEl,
313+
originalInfo,
314+
ctx,
315+
vault,
316+
settings,
317+
);
309318

310319
if (settings.debug) {
311320
console.log(

styles.css

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
}
2222
.w {
2323
line-height: 1.4;
24-
font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI',
25-
Roboto, 'Helvetica Neue', Arial, sans-serif;
24+
font-family:
25+
-apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto,
26+
'Helvetica Neue', Arial, sans-serif;
2627
font-weight: 400;
2728
font-size: 15px;
2829
color: inherit;
@@ -213,8 +214,9 @@
213214
}
214215
}
215216
._ffsa {
216-
font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI',
217-
Roboto, 'Helvetica Neue', Arial, sans-serif;
217+
font-family:
218+
-apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto,
219+
'Helvetica Neue', Arial, sans-serif;
218220
}
219221
._fwn {
220222
font-weight: 400;
@@ -347,7 +349,8 @@
347349

348350
/* Button styling */
349351
.refresh-button,
350-
.copy-button {
352+
.copy-button,
353+
.delete-button {
351354
cursor: pointer;
352355
}
353356

@@ -358,6 +361,7 @@
358361

359362
/* Full opacity when hovering over the buttons */
360363
.refresh-button:hover,
361-
.copy-button:hover {
364+
.copy-button:hover,
365+
.delete-button:hover {
362366
opacity: 1 !important;
363367
}

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
"2.8.6": "0.12.0",
5555
"2.9.0": "0.12.0",
5656
"2.9.1": "0.12.0",
57-
"2.9.2": "0.12.0"
57+
"2.9.2": "0.12.0",
58+
"2.9.3": "0.12.0"
5859
}

0 commit comments

Comments
 (0)