From 29d8632185caa8d003cb9e4b2accff1ea94f4f35 Mon Sep 17 00:00:00 2001 From: shkomg Date: Tue, 3 Nov 2026 16:55:43 +0100 Subject: [PATCH 1/2] fix(angular): keep nested custom components inside their parent element A custom component nested in a paragraph, list item or another component was mounted next to the outermost node instead of inside its parent. The child node now moves into its parent before change detection, and its custom component is placed right after it, since the view container anchor stays where the node was first attached. --- .../src/components/markdown-node.component.ts | 13 ++++++- .../test/nested-components.test.ts | 38 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/comark-angular/src/components/markdown-node.component.ts b/packages/comark-angular/src/components/markdown-node.component.ts index f6c381185..d0e435a64 100644 --- a/packages/comark-angular/src/components/markdown-node.component.ts +++ b/packages/comark-angular/src/components/markdown-node.component.ts @@ -323,6 +323,14 @@ export class MarkdownNode implements OnChanges { // Attach to DOM and detect changes this.vcr.insert(componentRef.hostView) + + // The container's anchor stays where this node was first attached, so when this node + // was moved into a parent element, place the component right after this node's host. + const hostEl = this.elementRef.nativeElement as HTMLElement + const hostParent = this.renderer.parentNode(hostEl) + if (hostParent) { + this.renderer.insertBefore(hostParent, componentRef.location.nativeElement, this.renderer.nextSibling(hostEl)) + } componentRef.changeDetectorRef.detectChanges() } @@ -368,12 +376,13 @@ export class MarkdownNode implements OnChanges { componentRef.setInput('components', this.components) componentRef.setInput('renderData', renderData) componentRef.setInput('parent', this.node) - componentRef.changeDetectorRef.detectChanges() - // Move the component's host element into the parent + // Move the component's host element into the parent before change detection, + // so the custom component it inserts lands next to it inside `parentEl` const nativeEl = componentRef.location.nativeElement as HTMLElement nativeEl.style.display = 'contents' this.renderer.appendChild(parentEl, nativeEl) + componentRef.changeDetectorRef.detectChanges() } catch (error) { componentRef?.destroy() console.error(`Failed to render custom component "${childTag}"`, error) diff --git a/packages/comark-angular/test/nested-components.test.ts b/packages/comark-angular/test/nested-components.test.ts index c925b39ed..226f50035 100644 --- a/packages/comark-angular/test/nested-components.test.ts +++ b/packages/comark-angular/test/nested-components.test.ts @@ -133,3 +133,41 @@ describe('nested components', () => { expect(html).toContain('>Ahad') }) }) + +/** ProsePre — a custom code block that wraps the rendered `
`. */
+class ProsePre {}
+Component({
+  selector: 'app-prose-pre',
+  standalone: true,
+  template: `
`, +})(ProsePre) + +/** Box — a block component that projects its children. */ +class Box {} +Component({ + selector: 'app-box', + standalone: true, + template: `
`, +})(Box) + +describe('nested component placement', () => { + it('keeps an inline component inside its paragraph, between the surrounding text', async () => { + const html = await renderMarkdown('Before :badge{name="x"} after.') + + expect(html).toMatch(/

Before .*x<\/span>.* after\.<\/p>/) + }) + + it('keeps a custom code block inside its list item', async () => { + const html = await renderMarkdown('- item\n\n ```js\n code\n ```\n', { + ProsePre: ProsePre as Type, + }) + + expect(html).toMatch(/

  • .*
    .*code.*<\/div>.*<\/li>/s) + }) + + it('keeps a nested block component inside its parent component', async () => { + const html = await renderMarkdown('::box\nOuter\n :::box\n Inner\n :::\n::\n', { box: Box as Type }) + + expect(html).toMatch(/

    Outer<\/p>.*

    Inner<\/div>.*<\/div>/s) + }) +}) From 329d8d8238d85264637e4973fa7b0ac6737f3069 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 4 Nov 2026 11:01:22 +0100 Subject: [PATCH 2/2] test: update bundle size snapshot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refresh the `@comark/angular` bundle size inline snapshot (56.5k → 56.8k) to account for the nested component rendering changes in this PR. --- test/bundle.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bundle.test.ts b/test/bundle.test.ts index fee333412..6239e69d4 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -60,7 +60,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { expect(report).toMatchInlineSnapshot(` { - "@comark/angular": "56.5k (72 files)", + "@comark/angular": "56.8k (72 files)", "@comark/ansi": "37.3k (98 files)", "@comark/html": "15.6k (58 files)", "@comark/nuxt": "11.9k (58 files)",