okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
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 ({ 10stdin :{ 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' }, 11bundle :true , write :false , platform :'node' , format :'cjs' , jsx :'automatic' , external :[ 'katex' , 'katex/dist/katex.min.css' ], 12plugins :[{ name :'stub' , setup ( b ) { 13b . onResolve ({ filter :/ ^\.\/ui$ / }, () => ({ path :'ui' , namespace :'test-stub' })); 14b . 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' , () => { 23const soft = html ( 'Line one\nLine two' ); 24assert . equal ( paragraphs ( soft ), 1 ); assert . match ( soft , / Line one Line two / ); 25assert . equal ( paragraphs ( html ( 'One\n\nTwo' )), 2 ); 26}); 27test ( 'two trailing spaces or a backslash keep a line break' , () => { 28assert . match ( html ( 'One \nTwo' ), / One<br\/>Two / ); 29assert . 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' , () => { 32const out = html ( 'Intro\n- a\n- b\n## Head\ntext\n```\ncode\n```' ); 33assert . match ( out , / <p>Intro<\/p><ul> / ); assert . match ( out , / <h2>Head<\/h2><p>text<\/p> / ); 34});