Skip to content

Commit

Permalink
Add HTML parsing via Fatdown bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
linc committed Oct 23, 2022
1 parent 8c96383 commit cadbd2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public function toTextFormatter(?string $format, ?string $text): string
switch ($format) {
case 'Html':
case 'Wysiwyg':
case 'Raw': // Unfiltered
return self::wrap('r', $this->fixRawMentions($text));
case 'BBCode':
return $this->fixRawMentions(BBCode::parse($text));
case 'Raw': // Unfiltered — these could break.
case 'Markdown':
// Fatdown bundle allows (filtered) HTML.
return $this->fixRawMentions(Markdown::parse($text));
case 'BBCode':
return $this->fixRawMentions(BBCode::parse($text));
case 'Rich': // Quill
return self::wrap('r', self::dequill($text));
case 'Text':
Expand Down
24 changes: 20 additions & 4 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PorterTest;

use nadar\quill\Lexer as Quill;
use s9e\TextFormatter\Bundles\Fatdown as Markdown;
use PHPUnit\Framework\TestCase;
use Porter\Parser\Flarum\ImageEmbed;

Expand All @@ -26,9 +27,24 @@ public function testQuillCanParseExternalEmbed(): void
'[upl-image-preview url=https://example.com/uploads/779/8C8NUCDYD6ZW.png]</UPL-IMAGE-PREVIEW>';
$this->assertStringContainsString($expected, $result);
}
// postid 227957

// postid 228220
//[{"insert":{"embed-external":{"data":{"recordID":227954,"recordType":"comment","body":"<p><a href=\"https:\/\/pmono.no\/discussion\/comment\/227952#Comment_227952\" rel=\"nofollow\">https:\/\/pmono.no\/discussion\/comment\/227952#Comment_227952<\/a><\/p><p>Er bare forn&aelig;rmet for at jeg ikke er med p&aring; skjemaet ditt.<\/p>","bodyRaw":[{"insert":"https:\/\/pmono.no\/discussion\/comment\/227952#Comment_227952","attributes":{"link":"https:\/\/pmono.no\/discussion\/comment\/227952#Comment_227952"}},{"insert":"\n"},{"insert":"Er bare fornærmet for at jeg ikke er med på skjemaet ditt. \n"}],"format":"Rich","dateInserted":"2021-10-11T11:41:46+00:00","insertUser":{"userID":2,"name":"thoreirik","photoUrl":"https:\/\/pmono.no\/uploads\/userpics\/882\/n4DN33MVJ31PH.jpg","dateLastActive":"2021-10-15T08:49:18+00:00"},"displayOptions":{"showUserLabel":false,"showCompactUserInfo":true,"showDiscussionLink":false,"showPostLink":false,"showCategoryLink":false,"renderFullContent":false,"expandByDefault":false},"url":"https:\/\/pmono.no\/discussion\/comment\/227954#Comment_227954","embedType":"quote"},"loaderData":{"type":"link","link":"https:\/\/pmono.no\/discussion\/comment\/227954#Comment_227954"}}}},{"insert":"Ble ferdig med skjemaet i går. Du kom akkurat inn på listen i den siste runden jeg la inn, som var årets første runde hvor du gav 12 poeng til Penguin Cafe Orchestra "},{"insert":{"emoji":{"emojiChar":"❤️"}}},{"insert":"\nHer er en rangering av hvor glad jeg er i dere i pgp-sammenheng:\n"},{"insert":{"mention":{"name":"MrHorse","userID":949}}},{"insert":" 6 (4 mottatt\/ 2 gitt)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"BorlängeHangover","userID":34}}},{"insert":" 5 (1 \/4)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"oddish","userID":903}}},{"insert":" 5 (2\/3)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"Stenseng","userID":30}}},{"insert":" 5 (4\/1)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"kebbster","userID":925}}},{"insert":" 4 (3\/1)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"Mytteristen","userID":927}}},{"insert":" 4 (1\/ 3)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"Erik","userID":17}}},{"insert":" 4 (0\/4)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"Sveinar_Finnebråten","userID":926}}},{"insert":" 3 (1\/2)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"Lillomarkstraveren","userID":951}}},{"insert":" 3 (3\/0)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"ArneKr","userID":3}}},{"insert":" 3 (2\/1)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"dogballs","userID":53}}},{"insert":" og "},{"insert":{"mention":{"name":"AG3","userID":78}}},{"insert":" 2 (0\/2)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"kenneth","userID":11}}},{"insert":" 2 (1\/1)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"ChillMagneBondevik","userID":914}}},{"insert":", "},{"insert":{"mention":{"name":"knøtten","userID":941}}},{"insert":", og "},{"attributes":{"mention-autocomplete":true},"insert":{"mention":{"name":"blomert","userID":203}}},{"attributes":{"mention-autocomplete":true},"insert":" 2 (2\/0)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"Jostein","userID":1}}},{"insert":", "},{"insert":{"mention":{"name":"Bjørn_Stad","userID":22}}},{"insert":", "},{"insert":{"mention":{"name":"Erikhans","userID":207}}},{"insert":", "},{"insert":{"mention":{"name":"Kyrrebust","userID":945}}},{"insert":" og "},{"insert":{"mention":{"name":"tilion","userID":16}}},{"insert":" 1 (0\/1)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":{"mention":{"name":"thoreirik","userID":2}}},{"insert":", "},{"insert":{"mention":{"name":"Hoa","userID":950}}},{"insert":", "},{"insert":{"mention":{"name":"kvan","userID":944}}},{"insert":" og "},{"insert":{"mention":{"name":"bubastis","userID":8}}},{"insert":" 1(1\/0)"},{"attributes":{"list":{"depth":0,"type":"ordered"}},"insert":"\n"},{"insert":"Gledelig overrasket over "},{"insert":{"mention":{"name":"BorlängeHangover","userID":34}}},{"insert":" og "},{"insert":{"mention":{"name":"Stenseng","userID":30}}},{"insert":". Tilsvarende skuffet over mange i motsatt ende av tabellen.\n\n"}]

/**
* @covers Formatter::toTextFormatter
*/
public function testFatdownCanParseHtml(): void
{
$stored = '<b>Avant </b>de poster dans le forum la présentation est <b><u>obligatoire</u> </b>ici :&nbsp;
<a rel="nofollow" href="https://routeur4g.fr/discussions/categories/pr%C3%A9sentation">Présentation</a>
&nbsp;avec dans le titre le pseudo.<br><span>Cela permet d\'en savoir plus sur votre matériel/lieu de
vie/niveau technique. Pas besoin de faire un roman mais expliquez pourquoi vous en êtes venu à la 4G nous
permet de vous donner de bons conseils.&nbsp;<br></span>';
$stored = Markdown::parse($stored);
$result = Markdown::render($stored);
$expected = '<p><b>Avant </b>de poster dans le forum la présentation est <b><u>obligatoire</u> </b>ici : 
<a href="https://routeur4g.fr/discussions/categories/pr%C3%A9sentation">Présentation</a>
 avec dans le titre le pseudo.<br><span>Cela permet d’en savoir plus sur votre matériel/lieu de
vie/niveau technique. Pas besoin de faire un roman mais expliquez pourquoi vous en êtes venu à la 4G nous
permet de vous donner de bons conseils. <br></span></p>';
$this->assertStringContainsString($expected, $result);
}
}

0 comments on commit cadbd2f

Please sign in to comment.