Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141import { readFileSync } from 'node:fs';import { describe, expect, it } from 'vitest';import { renderGuide } from './guide.ts';
const GUIDES_URL = 'https://tangled.org/katsuricata.com/Fablewright/blob/main/docs/';
describe('renderGuide', () => { it('turns a heading and a wrapped paragraph into a sentence', () => { const html = renderGuide('# Use Fablewright\n\nThis guide is for the\nwriter.\n');
expect(html).toBe('<h1>Use Fablewright</h1>\n<p>This guide is for the writer.</p>'); });
it('keeps heading levels', () => { const html = renderGuide('## Start\n\n### When a save fails\n');
expect(html).toContain('<h2>Start</h2>'); expect(html).toContain('<h3>When a save fails</h3>'); });
it('renders bullets, numbered steps, and a wrapped item', () => { const html = renderGuide( '1. Open the address.\n2. Create an account.\n\n- **Details** changes the title\n and the subtitle.\n- **Export** downloads the project.\n' );
expect(html).toContain('<ol><li>Open the address.</li><li>Create an account.</li></ol>'); expect(html).toContain( '<ul><li><strong>Details</strong> changes the title and the subtitle.</li><li><strong>Export</strong> downloads the project.</li></ul>' ); });
it('formats bold, italic and inline code', () => { const html = renderGuide('Press `Ctrl` + `S` for **bold** and *italic*.\n');
expect(html).toBe( '<p>Press <code>Ctrl</code> + <code>S</code> for <strong>bold</strong> and <em>italic</em>.</p>' ); });
it('formats underscore italics, as Prettier writes them', () => { const html = renderGuide('Open _Your account_ from the header.\n');
expect(html).toBe('<p>Open <em>Your account</em> from the header.</p>'); });
it('leaves an identifier and the text inside a code span alone', () => { const html = renderGuide('Set a_b_c and `x_y_z` and `2*3*4`.\n');
expect(html).toBe('<p>Set a_b_c and <code>x_y_z</code> and <code>2*3*4</code>.</p>'); });
it('treats the document as text, never as markup', () => { const html = renderGuide('<script>alert(1)</script> and **<b>bold</b>**\n');
expect(html).not.toContain('<script>'); expect(html).not.toContain('<b>'); expect(html).toContain('<script>'); expect(html).toContain('<strong><b>bold</b></strong>'); });
it('keeps ampersands and literal HTML entities as text', () => { expect(renderGuide('Fish & chips and <literal>.')).toBe( '<p>Fish & chips and &lt;literal&gt;.</p>' ); });
it.each(['http', 'https'])('keeps a %s link to another site', (scheme) => { const html = renderGuide(`Read [the source](${scheme}://example.test/docs).\n`);
expect(html).toContain( `<a href="${scheme}://example.test/docs" rel="noreferrer">the source</a>` ); });
it.each(['installation.md', 'operations.md', 'recovery.md', 'import-export.md'])( 'links the companion guide %s to its repository page', (file) => { expect(renderGuide(`Read [the guide](${file}).\n`)).toBe( `<p>Read <a href="${GUIDES_URL}${file}" rel="noreferrer">the guide</a>.</p>` ); } );
it('keeps a section fragment on a companion guide link', () => { expect(renderGuide('Read [backups](operations.md#back-up).')).toBe( `<p>Read <a href="${GUIDES_URL}operations.md#back-up" rel="noreferrer">backups</a>.</p>` ); });
it.each([ 'missing.md', '../operations.md', '/operations.md', '//example.test/operations.md', 'operations.md?redirect=elsewhere', 'data:text/html,unsafe', 'javascript:https://example.test', 'prefixhttps://example.test', 'ftp://example.test' ])('leaves unsupported destinations as text: %s', (href) => { expect(renderGuide(`Read [the guide](${href}).`)).toBe('<p>Read the guide.</p>'); });
it('escapes quotes in a companion guide fragment', () => { const html = renderGuide('[backups](operations.md#back-up"autofocus="true)');
expect(html).toBe( `<p><a href="${GUIDES_URL}operations.md#back-up"autofocus="true" rel="noreferrer">backups</a></p>` ); });
it('links every companion guide referenced by the shipped writer guide', () => { const markdown = readFileSync( new URL('../../docs/using-fablewright.md', import.meta.url), 'utf8' ); const html = renderGuide(markdown); const references = [...markdown.matchAll(/\[[^\]]+\]\(([^)\s]+)\)/g)] .map((match) => match[1]) .filter((href) => !/^https?:\/\//.test(href));
expect(references.length).toBeGreaterThan(0); for (const href of references) { expect(html).toContain(`href="${GUIDES_URL}${href}"`); } });
it('refuses a script address', () => { const html = renderGuide('[press me](javascript:alert(1))\n');
expect(html).not.toContain('href'); expect(html).toContain('press me'); });
it('leaves an unpaired asterisk alone', () => { const html = renderGuide('A 5 * 4 box, and *one* emphasis.\n');
expect(html).toBe('<p>A 5 * 4 box, and <em>one</em> emphasis.</p>'); });});