rob-revisited/ide/tests/HtmlToMarkdownTest.php

65 lines
4.3 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Board\HtmlToMarkdown;
use PHPUnit\Framework\TestCase;
/** A formatted document pasted into a card becomes its Markdown: formatting and pictures kept, anything dangerous left out. */
final class HtmlToMarkdownTest extends TestCase
{
public function testHeadingsParagraphsAndInlineFormatting(): void
{
$md = HtmlToMarkdown::convert('<h1>Boss</h1><p>The <b>boss</b> jumps <i>twice</i>, <u>really</u> <s>never</s> <code>jump()</code>.<br>Second line</p>'
. '<h3>Phase 2</h3><p>See <a href="https://example.com/a b">the doc</a> and <a href="javascript:alert(1)">this</a>.</p><hr><p>1. not a list * star</p>');
self::assertSame("# Boss\n\nThe **boss** jumps *twice*, <u>really</u> ~~never~~ `jump()`.\nSecond line\n\n### Phase 2\n\n"
. "See [the doc](https://example.com/a%20b) and this.\n\n---\n\n1\\. not a list \\* star\n", $md);
}
public function testNestedListsQuotesAndCode(): void
{
$md = HtmlToMarkdown::convert('<ul><li>One<ul><li>One a</li><li>One b<ol><li>deep</li></ol></li></ul></li><li><p>Two</p></li></ul>'
. '<ol start="3"><li>three</li><li>four</li></ol><blockquote><p>Quoted</p><blockquote><p>twice</p></blockquote></blockquote><pre>' . "if (a) {\n b();\n}</pre>");
self::assertSame("- One\n - One a\n - One b\n 1. deep\n- Two\n\n3. three\n4. four\n\n> Quoted\n>\n> > twice\n\n```\nif (a) {\n b();\n}\n```\n", $md);
}
public function testTables(): void
{
$md = HtmlToMarkdown::convert('<table><thead><tr><th>Level</th><th>Boss</th></tr></thead><tbody><tr><td><b>1</b></td><td>Crab | big</td></tr>'
. '<tr><td colspan="2">two<br>lines</td></tr></tbody></table>');
self::assertSame("| Level | Boss |\n| --- | --- |\n| **1** | Crab \\| big |\n| two<br>lines | |\n", $md);
}
public function testDangerousHtmlIsLeftOut(): void
{
$md = HtmlToMarkdown::convert('<script>alert(1)</script><style>p{}</style><p onclick="x()">Hi <iframe src="https://evil"></iframe><img src="x" onerror="alert(1)">'
. '<a href="data:text/html,x">data</a> <a href=" JAVASCRIPT:alert(1)">js</a> &lt;script&gt;</p><form><input value="secret"></form>');
self::assertSame("Hi data js \\<script\\>\n", $md);
}
public function testPicturesBecomeWhatTheCallerSays(): void
{
$html = '<p>Before <img src="data:image/png;base64,AAAA" alt="shot [1]"> after</p><p><img src="https://cdn.example/a.png"><img src="file:///C:/x.png"></p>';
self::assertSame(['data:image/png;base64,AAAA', 'https://cdn.example/a.png', 'file:///C:/x.png'], HtmlToMarkdown::imageSources($html));
self::assertSame("Before after\n\n![](https://cdn.example/a.png)\n", HtmlToMarkdown::convert($html), 'default: http(s) addresses only');
$md = HtmlToMarkdown::convert($html, fn (string $src) => match (true) {
str_starts_with($src, 'data:') => 'attachment:shot.png',
str_starts_with($src, 'https:') => 'attachment:a.png',
default => null,
});
self::assertSame("Before ![shot 1](attachment:shot.png) after\n\n![](attachment:a.png)\n", $md);
}
public function testGoogleDocsAndWordShapes(): void
{
// Google Docs: everything in a <b style="font-weight:normal">, bold and italic as styles, colours
$docs = '<meta charset="utf-8"><b style="font-weight:normal;" id="docs-internal-guid-1"><p dir="ltr"><span style="font-weight:700;">Bold</span>'
. '<span style="font-weight:400;"> plain </span><span style="font-style:italic;color:#ff0000;">red</span><span style="color:#000000;"> black</span></p>'
. '<ul><li dir="ltr"><p dir="ltr"><span>item</span></p></li></ul></b>';
self::assertSame("**Bold** plain *<span style=\"color:#ff0000\">red</span>* black\n\n- item\n", HtmlToMarkdown::convert($docs));
// Word: conditional comments, o:p, MsoNormal paragraphs
$word = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"><head><style>p.MsoNormal{}</style></head><body><!--[if gte mso 9]><xml>x</xml><![endif]-->"
. "<p class=MsoNormal><b>Title<o:p></o:p></b></p><p class=MsoNormal>Text&nbsp;here<o:p></o:p></p></body></html>";
self::assertSame("**Title**\n\nText here\n", HtmlToMarkdown::convert($word));
}
}