@splendidlabz/svelte 4.8.2 → 4.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @splendidlabz/svelte
2
2
 
3
+ ## 4.8.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Remove redundant `<p>` tag stripping in `Markdown` components. The `markdown-it` `renderInline` method never produces `<p>` tags, so the `.replace('<p>', '').replace('</p>', '')` calls after it were dead code.
8
+
3
9
  ## 4.8.2
4
10
 
5
11
  ### Patch Changes
@@ -0,0 +1,47 @@
1
+ import { beforeAll, describe, expect, test } from 'vitest'
2
+ import { loadSvelteComponent, renderComponent } from '../../test-utils.js'
3
+
4
+ let Markdown
5
+
6
+ beforeAll(async () => {
7
+ Markdown = await loadSvelteComponent(new URL('./Markdown.svelte', import.meta.url))
8
+ })
9
+
10
+ describe('Markdown', () => {
11
+ describe('block (default)', () => {
12
+ test('wraps content in <p>', () => {
13
+ const result = renderComponent(Markdown, { content: 'hello' })
14
+ expect(result).toBe('<p>hello</p>')
15
+ })
16
+
17
+ test('renders bold', () => {
18
+ const result = renderComponent(Markdown, { content: '**bold**' })
19
+ expect(result).toBe('<p><strong>bold</strong></p>')
20
+ })
21
+ })
22
+
23
+ describe('inline', () => {
24
+ test('does not produce <p> tags', () => {
25
+ const result = renderComponent(Markdown, { content: 'hello', inline: true })
26
+ expect(result).not.toContain('<p>')
27
+ expect(result).not.toContain('</p>')
28
+ })
29
+
30
+ test('renders bold without <p>', () => {
31
+ const result = renderComponent(Markdown, { content: '**bold**', inline: true })
32
+ expect(result).toBe('<strong>bold</strong>')
33
+ })
34
+ })
35
+
36
+ describe('edge cases', () => {
37
+ test('returns empty for empty content', () => {
38
+ const result = renderComponent(Markdown, { content: '' })
39
+ expect(result).toBe('')
40
+ })
41
+
42
+ test('returns empty for undefined content', () => {
43
+ const result = renderComponent(Markdown, {})
44
+ expect(result).toBe('')
45
+ })
46
+ })
47
+ })
@@ -8,16 +8,7 @@
8
8
  <script>
9
9
  import { markdown } from '@splendidlabz/utils'
10
10
  const { content, inline = false } = $props()
11
-
12
- // svelte-ignore non_reactive_update
13
- let html = ''
14
- html = markdown(content, { inline })
15
- if (inline) {
16
- // Replace enclosing <p> tags with <span> tags
17
- // Makes it possible to use other tags like <em> without disrupting the layout
18
- html = html.replace('<p>', '<span>').replace('</p>', '</span>')
19
- html = `<span> ${html} </span>`
20
- }
11
+ const html = markdown(content, { inline })
21
12
  </script>
22
13
 
23
14
  <!-- eslint-disable-next-line svelte/no-at-html-tags -->
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@splendidlabz/svelte",
3
3
  "type": "module",
4
- "version": "4.8.2",
4
+ "version": "4.8.3",
5
5
  "sideEffects": false,
6
6
  "homepage": "https://splendidlabz.com/docs/svelte",
7
7
  "scripts": {
8
8
  "lint": "eslint . --fix",
9
+ "prepack": "vitest run",
9
10
  "release": "np --no-publish && npm publish",
11
+ "test": "vitest run",
12
+ "test:watch": "vitest",
13
+ "test:ui": "vitest --ui",
10
14
  "check": "svelte-check --tsconfig ./jsconfig.json",
11
15
  "check:watch": "svelte-check --tsconfig ./jsconfig.json --watch"
12
16
  },
package/test-utils.js ADDED
@@ -0,0 +1,44 @@
1
+ import { readFile, unlink, writeFile } from 'node:fs/promises'
2
+ import path from 'node:path'
3
+ import { fileURLToPath, pathToFileURL } from 'node:url'
4
+ import { compile } from 'svelte/compiler'
5
+ import { render } from 'svelte/server'
6
+
7
+ /**
8
+ * Compiles and loads a Svelte component for server-side rendering in tests.
9
+ * @param {string} fileUrl - Import meta URL of the component (e.g. `new URL('./Foo.svelte', import.meta.url)`)
10
+ * @returns {Promise<object>} The compiled Svelte component
11
+ */
12
+ export async function loadSvelteComponent(fileUrl) {
13
+ const filePath = fileURLToPath(fileUrl)
14
+ const source = await readFile(filePath, 'utf8')
15
+ const compiled = compile(source, {
16
+ filename: path.basename(filePath),
17
+ generate: 'server',
18
+ })
19
+
20
+ const tempPath = path.join(
21
+ path.dirname(filePath),
22
+ `.spec.${process.pid}.${Date.now()}.mjs`,
23
+ )
24
+
25
+ await writeFile(tempPath, compiled.js.code)
26
+
27
+ try {
28
+ const mod = await import(pathToFileURL(tempPath).href)
29
+ return mod.default
30
+ } finally {
31
+ await unlink(tempPath)
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Renders a Svelte component to an HTML string, stripping Svelte's internal comment markers.
37
+ * @param {object} Component - Compiled Svelte component
38
+ * @param {object} [props={}] - Props to pass to the component
39
+ * @returns {string} Rendered HTML
40
+ */
41
+ export function renderComponent(Component, props = {}) {
42
+ const { body } = render(Component, { props })
43
+ return body.replace(/<!--[^]*?-->/g, '').trim()
44
+ }