s9e / TextFormatter

Text formatting library that supports BBCode, HTML and other markup via plugins. Handles emoticons, censors words, automatically embeds media and more.
MIT License
233 stars 36 forks source link

customize hljs load url/CDN #193

Closed frankli0324 closed 2 years ago

frankli0324 commented 2 years ago

could jsdelivr be replaced? ref https://github.com/flarum/framework/issues/3440

JoshyPHP commented 2 years ago

Yes.

frankli0324 commented 2 years ago

it would be nicer if it's somehow configurable...

JoshyPHP commented 2 years ago

If your goal is to display emoji from a specific CDN, the simplest way would be to set your own template via $configurator->Emoji->getTag()->template. If you want to keep the original template and only change the image's source, you can modify the existing template using DOM operations. For example:

$configurator = new s9e\TextFormatter\Configurator;
$dom = $configurator->Emoji->getTag()->template->asDOM();
foreach ($dom->query('//img') as $img)
{
    $img->setAttribute('src', '/path/to/emoji/{@seq}.svg');
}
$dom->saveChanges();

extract($configurator->finalize());

$text = "\u{1F600}";
$xml  = $parser->parse($text);
$html = $renderer->render($xml);

die("$html\n");
<img alt="😀" class="emoji" draggable="false" src="/path/to/emoji/1f600.svg">
frankli0324 commented 2 years ago

actually I already dealt with emoji, and I'm currently struggling with code highlighting

JoshyPHP commented 2 years ago

Then it's the same but with the CODE tag.

frankli0324 commented 2 years ago

Thanks for the suggestion( but.... I didn't... hmmmm like it that much? it's not ... can't explain(((:P thanks anyway

also, I'm just a user of flarum, and followed the jsdelivr url to this repo. https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.28/loader.min.js seems to be hard coded

JoshyPHP commented 2 years ago

Yes, the URL to highlight.js can be changed at the time of the BBCode's creation (see below) but the URL to hljs-loader is hardcoded. Even if there were mirrors for the library, you'd have to match the exact version otherwise the integrity check would fail and the thing would break silently. At that point, it's simpler to just create your own BBCode than try to modify this one.

$configurator = new s9e\TextFormatter\Configurator;
$configurator->BBCodes->addFromRepository(
    'CODE',
    'default',
    ['url' => 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/']
);

extract($configurator->finalize());

$text = '[code]printf("%s", $var);[/code]';
$xml  = $parser->parse($text);
$html = $renderer->render($xml);

die("$html\n");
<pre><code>printf("%s", $var);</code><script async="" crossorigin="anonymous" data-hljs-style="github" data-hljs-url="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/" integrity="sha384-fQc6882DqpMzcLWJbK07+Xnd8dceuG+q15iFvx+m/bAzjiBJeLnbNfaiXYzJBEn/" onload="hljsLoader.highlightBlocks(this.parentNode)" src="https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.28/loader.min.js"></script></pre>
frankli0324 commented 2 years ago

this is in fact more of a feature request rather than seeking help... well, this wouldn't be solved then? also, why not publishing hljs-loader on npm? to make CDNs happy