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) + }) +}) 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)",