okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
2.1 KiB34 linesraw
1// #3818: a single newline inside a paragraph is a space; a blank line starts a new paragraph; two trailing spaces or a backslash is a line break.
2import assert from 'node:assert/strict';
3import test from 'node:test';
4import { resolve } from 'node:path';
5import { createRequire } from 'node:module';
6import { build } from 'esbuild';
7
8const root = resolve(import.meta.dirname, '..');
9const bundle = await build({
10  stdin: { contents: `import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { Markdown } from './src/components/Markdown.tsx'; export const html = text => renderToStaticMarkup(React.createElement(Markdown, { text }));`, resolveDir: root, loader: 'ts' },
11  bundle: true, write: false, platform: 'node', format: 'cjs', jsx: 'automatic', external: ['katex', 'katex/dist/katex.min.css'],
12  plugins: [{ name: 'stub', setup(b) {
13    b.onResolve({ filter: /^\.\/ui$/ }, () => ({ path: 'ui', namespace: 'test-stub' }));
14    b.onLoad({ filter: /.*/, namespace: 'test-stub' }, () => ({ contents: 'export const IconBtn = () => null; export const Btn = () => null; export const Icon = () => null;' }));
15  } }],
16});
17const mod = { exports: {} };
18new Function('require', 'module', 'exports', bundle.outputFiles[0].text)(createRequire(import.meta.url), mod, mod.exports);
19const { html } = mod.exports;
20const paragraphs = out => (out.match(/<p>/g) ?? []).length;
21
22test('a single newline is a soft break, a blank line a new paragraph', () => {
23  const soft = html('Line one\nLine two');
24  assert.equal(paragraphs(soft), 1); assert.match(soft, /Line one Line two/);
25  assert.equal(paragraphs(html('One\n\nTwo')), 2);
26});
27test('two trailing spaces or a backslash keep a line break', () => {
28  assert.match(html('One  \nTwo'), /One<br\/>Two/);
29  assert.match(html('One' + String.fromCharCode(92) + String.fromCharCode(10) + 'Two'), /One<br\/>Two/);
30});
31test('a list, heading or code block right after a line still starts its own block', () => {
32  const out = html('Intro\n- a\n- b\n## Head\ntext\n```\ncode\n```');
33  assert.match(out, /<p>Intro<\/p><ul>/); assert.match(out, /<h2>Head<\/h2><p>text<\/p>/);
34});