2019-12-26 09:14:04 +01:00
< ? php
/**
* Name : Markdown
* Description : Parse Markdown code when creating new items
* Version : 0.1
* Author : Michael Vogel < https :// pirati . ca / profile / heluecht >
*/
use Friendica\App ;
use Friendica\Core\Hook ;
use Friendica\Content\Text\Markdown ;
use Friendica\Core\Renderer ;
2020-01-18 16:50:56 +01:00
use Friendica\DI ;
2019-12-26 09:14:04 +01:00
function markdown_install () {
Hook :: register ( 'post_local_start' , __FILE__ , 'markdown_post_local_start' );
Hook :: register ( 'addon_settings' , __FILE__ , 'markdown_addon_settings' );
Hook :: register ( 'addon_settings_post' , __FILE__ , 'markdown_addon_settings_post' );
}
2023-01-14 03:16:09 +01:00
function markdown_addon_settings ( array & $data )
2019-12-26 09:14:04 +01:00
{
2022-10-20 23:51:49 +02:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2019-12-26 09:14:04 +01:00
return ;
}
2022-10-20 23:51:49 +02:00
$enabled = intval ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'markdown' , 'enabled' ));
2019-12-26 09:14:04 +01:00
2021-11-20 10:56:55 +01:00
$t = Renderer :: getMarkupTemplate ( 'settings.tpl' , 'addon/markdown/' );
$html = Renderer :: replaceMacros ( $t , [
'$enabled' => [ 'enabled' , DI :: l10n () -> t ( 'Enable Markdown parsing' ), $enabled , DI :: l10n () -> t ( 'If enabled, adds Markdown support to the Compose Post form.' )],
2019-12-26 09:14:04 +01:00
]);
2021-11-20 10:56:55 +01:00
$data = [
'addon' => 'markdown' ,
'title' => DI :: l10n () -> t ( 'Markdown Settings' ),
'html' => $html ,
];
2019-12-26 09:14:04 +01:00
}
2023-01-14 03:16:09 +01:00
function markdown_addon_settings_post ( array & $b )
2019-12-26 09:14:04 +01:00
{
2022-10-20 23:51:49 +02:00
if ( ! DI :: userSession () -> getLocalUserId () || empty ( $_POST [ 'markdown-submit' ])) {
2019-12-26 09:14:04 +01:00
return ;
}
2022-10-20 23:51:49 +02:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'markdown' , 'enabled' , intval ( $_POST [ 'enabled' ]));
2019-12-26 09:14:04 +01:00
}
2023-01-14 03:16:09 +01:00
function markdown_post_local_start ( & $request ) {
2022-10-20 23:51:49 +02:00
if ( empty ( $request [ 'body' ]) || ! DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'markdown' , 'enabled' )) {
2019-12-26 09:14:04 +01:00
return ;
}
2020-11-02 17:02:08 +01:00
// Escape elements that shouldn't be parsed
$request [ 'body' ] = \Friendica\Content\Text\BBCode :: performWithEscapedTags (
$request [ 'body' ],
[ 'code' , 'noparse' , 'nobb' , 'pre' , 'share' , 'url' , 'img' , 'bookmark' ,
'audio' , 'video' , 'youtube' , 'vimeo' , 'attachment' , 'iframe' , 'map' , 'mail' ],
function ( $body ) {
// Escape mentions which username can contain Markdown-like characters
// See https://github.com/friendica/friendica/issues/9486
return \Friendica\Util\Strings :: performWithEscapedBlocks ( $body , '/[@!][^@\s]+@[^\s]+\w/' , function ( $text ) {
2021-10-18 15:13:47 +02:00
// Markdown accepts literal HTML but we do not in post body, so we need to escape left chevrons
// (right chevrons are used for quoting in Markdown)
2021-10-14 08:11:53 +02:00
// See https://github.com/friendica/friendica/issues/10634
2021-10-18 15:13:47 +02:00
$text = strtr ( $text , [ '<' => '<' ]);
2024-02-10 13:00:36 +01:00
$text = str_replace ( '[*]' , '[li]' , $text );
2021-10-14 08:11:53 +02:00
2020-11-02 17:02:08 +01:00
return Markdown :: toBBCode ( $text );
});
}
);
2019-12-26 09:14:04 +01:00
}