Syntax Highlighting tool, Markdown guide, unread share badge, Open in menu

- Syntax Highlighting (/syntax): highlight.js tokens rendered with VS Code
  (Dark/Light Modern, One Dark Pro, Monokai, Dracula, GitHub) and Android
  Studio (Darcula, Dark, IntelliJ Light) colors; copy as rich text (VS Code
  clipboard HTML), download RTF, share. Format via Prettier/sql-formatter
  (lazy-loaded), the JSON printer, or built-in whitespace-only formatters
  for brace languages, Python and XML.
- Markdown: Guide dialog, source next to rendered output, with Insert.
- Pastes created by Share count as unread on the Pastebin nav badge.
- Paste view: Open in menu built from registry opensPaste tools.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
neil 2026-11-02 00:30:13 -08:00
parent f4232ef9b5
commit dc2b12a3c6
37 changed files with 3704 additions and 27 deletions

229
tests/code-format.test.mjs Normal file
View file

@ -0,0 +1,229 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { formatCode, formatXml } from '../app/static/js/tools/code-format.js';
import { tokenize, toLines, toHtml, toRtf, detect } from '../app/static/js/tools/highlight-core.js';
import { THEMES } from '../app/static/js/tools/highlight-themes.js';
const squash = (s) => s.replace(/\s+/g, '');
const SAMPLES = {
kotlin: `package demo
class Greeter(val name:String) : Base(){
// say hi
fun greet(times:Int=1):String
{
val s="hi, \${name} x"
if(times>1)
return s.repeat(times)
val list=listOf(1,2,3).map{it*2}
.filter { it>2 }
when(times){
1->println("one")
else->{ println("many") }
}
return s
}
}`,
java: `public class A{
@Override
public int f( int a,int b ){
int x=a+b ;
for(int i=0;i<10;i++){ x+=i; }
switch(x){
case 1:
x=2;
break;
default:
x=3;
}
/**
* Doc comment
* second line
*/
if(x==3){return -1;}
else{return x;}
}
}`,
csharp: `public class Foo {
public int Bar(int x) {
if (x > 0) {
return x;
} else {
return -x;
}
}
}`,
go: `package main
func main(){
x:=map[string]int{"a":1}
switch x["a"]{
case 1:
fmt.Println("one")
}
}`,
c: `#include <stdio.h>
int main(int argc,char **argv){
int *p=&argc;
if(argc>1){printf("%d\\n",*p);}
return 0;
}`,
python: `import os
def f(a,b = 2,*args,c:int=3,**kw):
x=a+b # comment
d={'k' : 1,'j':[1,2]}
if x>1 :
return d[ 'k' ][1:-1]
y = f(a ,
b=1)
return -x
`,
rust: `fn main(){
let v=vec![1,2,3];
let s:i32=v.iter().map(|x| x*2).sum();
for i in 0..=5 { println!("{}",i); }
}`,
};
for (const [lang, src] of Object.entries(SAMPLES)) {
test(`${lang}: only whitespace changes, and formatting is stable`, async () => {
const once = await formatCode(src, lang);
assert.equal(squash(once), squash(src), `${lang} changed non-whitespace:\n${once}`);
const twice = await formatCode(once, lang);
assert.equal(twice, once, `${lang} not idempotent:\n--- once\n${once}\n--- twice\n${twice}`);
});
}
test('kotlin output', async () => {
const out = await formatCode(SAMPLES.kotlin, 'kotlin');
assert.equal(out, `package demo
class Greeter(val name: String) : Base() {
// say hi
fun greet(times: Int = 1): String {
val s = "hi, \${name} x"
if (times > 1)
return s.repeat(times)
val list = listOf(1, 2, 3).map { it * 2 }
.filter { it > 2 }
when (times) {
1 -> println("one")
else -> { println("many") }
}
return s
}
}
`);
});
test('java output', async () => {
const out = await formatCode(SAMPLES.java, 'java');
assert.equal(out, `public class A {
@Override
public int f(int a, int b) {
int x = a + b;
for (int i = 0; i < 10; i++) { x += i; }
switch (x) {
case 1:
x = 2;
break;
default:
x = 3;
}
/**
* Doc comment
* second line
*/
if (x == 3) {return -1;}
else {return x;}
}
}
`);
});
test('c# uses Allman braces', async () => {
const out = await formatCode(SAMPLES.csharp, 'csharp');
assert.equal(out, `public class Foo
{
public int Bar(int x)
{
if (x > 0)
{
return x;
}
else
{
return -x;
}
}
}
`);
});
test('go: tabs, cases at switch level', async () => {
const out = await formatCode(SAMPLES.go, 'go');
assert.equal(out, 'package main\nfunc main() {\n\tx := map[string]int{"a": 1}\n\tswitch x["a"] {\n\tcase 1:\n\t\tfmt.Println("one")\n\t}\n}\n');
});
test('c: pointers untouched, preprocessor at column 0', async () => {
const out = await formatCode(SAMPLES.c, 'c');
assert.equal(out, '#include <stdio.h>\nint main(int argc, char **argv) {\n int *p = &argc;\n if (argc > 1) {printf("%d\\n", *p);}\n return 0;\n}\n');
});
test('python output', async () => {
const out = await formatCode(SAMPLES.python, 'python');
assert.equal(out, `import os
def f(a, b=2, *args, c: int = 3, **kw):
x = a + b # comment
d = {'k': 1, 'j': [1, 2]}
if x > 1:
return d['k'][1:-1]
y = f(a,
b=1)
return -x
`);
});
test('xml: Android Studio attribute wrapping', () => {
const out = formatXml('<?xml version="1.0"?><LinearLayout xmlns:android="http://x" android:orientation="vertical"><TextView android:id="@+id/t" android:text="hi"/><!-- c --><item>text</item><empty></empty></LinearLayout>');
assert.equal(out, `<?xml version="1.0"?>
<LinearLayout
xmlns:android="http://x"
android:orientation="vertical">
<TextView
android:id="@+id/t"
android:text="hi" />
<!-- c -->
<item>text</item>
<empty></empty>
</LinearLayout>
`);
});
test('json and sql and prettier', async () => {
assert.equal(await formatCode('{"a":[1,2]}', 'json'), '{\n "a": [\n 1,\n 2\n ]\n}\n');
assert.match(await formatCode('select a,b from t where x=1', 'sql'), /^select\n a,\n b\nfrom\n t\nwhere\n x = 1\n$/);
assert.equal(await formatCode('const a={b:1}', 'javascript'), 'const a = { b: 1 };\n');
assert.equal(await formatCode('let x:number=1', 'typescript'), 'let x: number = 1;\n');
assert.equal(await formatCode('a{color:red}', 'css'), 'a {\n color: red;\n}\n');
await assert.rejects(formatCode('const = ;', 'javascript'));
});
test('tokens, html and rtf', () => {
const lines = toLines(tokenize('val x = "a" // c\nfun f() {}', 'kotlin'));
assert.equal(lines.length, 2);
const theme = THEMES.find((t) => t.id === 'darcula');
const html = toHtml(lines, theme, { lineNumbers: true });
assert.match(html, /<span style="color:#CC7832;">val<\/span>/);
assert.match(html, /color:#6A8759;">&quot;a&quot;|color:#6A8759;">"a"/);
assert.match(html, /color:#606366;">1 </);
const rtf = toRtf(toLines(tokenize('s = "é😀 {x}"', 'python')), theme);
assert.match(rtf, /^\{\\rtf1/);
assert.match(rtf, /\\u233\?\\u-10179\?\\u-8704\? \\\{x\\\}/);
assert.equal(detect('fun main() { val x: Int = 1; println("$x") }'), 'kotlin');
// VS Code colors control keywords apart from declarations
const vs = THEMES.find((t) => t.id === 'vscode-dark');
const h2 = toHtml(toLines(tokenize('if (a) return 1; const b = 2;', 'javascript')), vs);
assert.match(h2, /#C586C0;">if</);
assert.match(h2, /#569CD6;">const</);
});