From 4b1040550311d8d6950352d960e41c91133ca51e Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Sat, 28 Oct 2023 19:14:18 +0200 Subject: [PATCH] Add config for default Markdown rendering (#42) * WIP RenderMarkdownByDefault https://github.com/ProfessionalWiki/ExternalContent/issues/37 * Added Code, Test and updated Readme Signed-off-by: Muhammad Yousuf Fazal * Fixes Signed-off-by: Muhammad Yousuf Fazal * Fixing the Incorrect Conflict Resolution Signed-off-by: Muhammad Yousuf Fazal * Minor Change Signed-off-by: Muhammad Yousuf Fazal * Another Fix Signed-off-by: Muhammad Yousuf Fazal * Fix Signed-off-by: Muhammad Yousuf Fazal --------- Signed-off-by: Muhammad Yousuf Fazal Co-authored-by: Muhammad Yousuf Fazal --- README.md | 15 ++++++++++++++ extension.json | 4 ++++ src/Domain/ContentRenderer/RendererConfig.php | 1 + src/Domain/WikiContentRendererFactory.php | 20 +++++-------------- src/EmbedExtensionFactory.php | 1 + src/UseCases/Embed/EmbedRequest.php | 1 + src/UseCases/Embed/EmbedRequestBuilder.php | 7 +++++++ src/UseCases/Embed/EmbedUseCase.php | 1 + .../Domain/WikiContentRendererFactoryTest.php | 5 +++++ .../Unit/UseCases/Embed/EmbedUseCaseTest.php | 1 + 10 files changed, 41 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ce28302..58b3655 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Parameters: * `lang`: (Optional) One of the [supported languages]. Only necessary if the language is not detected from the file extension. * `line`: (Optional) Show line numbers. * `specificLines`: (Optional) Show only specific lines. Can be a single line number or a range separated with a hyphen (-). Multiple line numbers or ranges can be separated by commas. +* `render`: (Optional) render as Markdown Examples: @@ -79,6 +80,10 @@ Render PHP file with specific lines: ``` {{#embed:https://example.com/fluffy/kittens.php|lang=php|specificLines=1-3,8}} ``` +Render file as Markdown: +``` +{{#embed:https://example.com/fluffy/kittens.php|render}} +``` ### Refreshing external content @@ -119,6 +124,16 @@ You can verify the extension was enabled successfully by opening your wikis Spec Configuration can be changed via [LocalSettings.php]. +### Render markdown by default + +Render markdown files rather than showing the markdown in a codeblock, unless the file is explicitly marked as code. + +Variable: `$wgExternalContentRenderMarkdownByDefault` + +Default: `true` + +Example: `false` - disables the default markdown render + ### Domain whitelist List of allowed domains to embed content from. Leave empty to have no restriction. diff --git a/extension.json b/extension.json index 08ad627..2a04f97 100644 --- a/extension.json +++ b/extension.json @@ -20,6 +20,10 @@ }, "config": { + "ExternalContentRenderMarkdownByDefault": { + "description": "Render markdown files rather than showing the markdown in a codeblock, unless the file is explicitly marked as code.", + "value": true + }, "ExternalContentDomainWhitelist": { "description": "List of allowed domains to embed content from. Leave empty to have no restriction.", "value": [] diff --git a/src/Domain/ContentRenderer/RendererConfig.php b/src/Domain/ContentRenderer/RendererConfig.php index d6e9502..ef9a0cf 100644 --- a/src/Domain/ContentRenderer/RendererConfig.php +++ b/src/Domain/ContentRenderer/RendererConfig.php @@ -11,6 +11,7 @@ public function __construct( public string $language, public bool $showLineNumbers, public string $showSpecificLines, + public bool $render, public bool $showEditButton ) { } diff --git a/src/Domain/WikiContentRendererFactory.php b/src/Domain/WikiContentRendererFactory.php index 40e39b6..1f51e8a 100644 --- a/src/Domain/WikiContentRendererFactory.php +++ b/src/Domain/WikiContentRendererFactory.php @@ -11,27 +11,17 @@ class WikiContentRendererFactory implements ContentRendererFactory { public function createContentRenderer( RendererConfig $config ): ContentRenderer { - if ( $config->language !== '' ) { + if ( $config->render ) { + return new MarkdownRenderer(); + } + else { return new CodeRenderer( - language: $config->language, + language: ( $config->language !== '' ) ? $config->language : $config->fileExtension, // TODO: Use an extension-to-language map, although common extensions already work. showLineNumbers: $config->showLineNumbers, showSpecificLines: $config->showSpecificLines, showEditButton: $config->showEditButton ); } - - // TODO: check config if this should be default behavior - if ( $config->fileExtension === 'md' ) { - return new MarkdownRenderer(); - } - - return new CodeRenderer( - language: $config->fileExtension, // TODO: Use an extension-to-language map, although common extensions already work. - showLineNumbers: $config->showLineNumbers, - showSpecificLines: $config->showSpecificLines, - showEditButton: $config->showEditButton - - ); } } diff --git a/src/EmbedExtensionFactory.php b/src/EmbedExtensionFactory.php index 1b806d3..82c1217 100644 --- a/src/EmbedExtensionFactory.php +++ b/src/EmbedExtensionFactory.php @@ -25,6 +25,7 @@ class EmbedExtensionFactory { public const DEFAULT_CONFIG = [ 'wgLanguageCode' => 'en', + 'wgExternalContentRenderMarkdownByDefault' => true, 'wgExternalContentDomainWhitelist' => [], 'wgExternalContentFileExtensionWhitelist' => [ 'md' ], 'wgExternalContentEnableEmbedFunction' => true, diff --git a/src/UseCases/Embed/EmbedRequest.php b/src/UseCases/Embed/EmbedRequest.php index 2a8886d..56158d8 100644 --- a/src/UseCases/Embed/EmbedRequest.php +++ b/src/UseCases/Embed/EmbedRequest.php @@ -11,6 +11,7 @@ public function __construct( public ?string $language, public ?bool $showLineNumbers, public ?string $showSpecificLines, + public ?bool $render, public bool $showEditButton ) { } diff --git a/src/UseCases/Embed/EmbedRequestBuilder.php b/src/UseCases/Embed/EmbedRequestBuilder.php index 3b00cf1..c44ab9f 100644 --- a/src/UseCases/Embed/EmbedRequestBuilder.php +++ b/src/UseCases/Embed/EmbedRequestBuilder.php @@ -4,6 +4,8 @@ namespace ProfessionalWiki\ExternalContent\UseCases\Embed; +use MediaWiki\MediaWikiServices; + class EmbedRequestBuilder { /** @@ -15,12 +17,17 @@ public static function argumentsToRequest( array $arguments, bool $showEditButto $language = $normalizedArguments['lang'] ?? null; $line = $normalizedArguments['line'] ?? null; $specificLines = $normalizedArguments['specificLines'] ?? null; + $render = $normalizedArguments['render'] ?? null; + + /** @var bool */ + $markdownByDefault = MediaWikiServices::getInstance()->getMainConfig()->get( 'ExternalContentRenderMarkdownByDefault' ); return new EmbedRequest( fileUrl: $arguments[0], language: is_string( $language ) ? $language : null, showLineNumbers: is_bool( $line ) ? $line : null, showSpecificLines: is_string( $specificLines ) ? $specificLines : null, + render: $markdownByDefault ? $markdownByDefault : ( is_bool( $render ) ? $render : null ), showEditButton: $showEditButton ); } diff --git a/src/UseCases/Embed/EmbedUseCase.php b/src/UseCases/Embed/EmbedUseCase.php index 6712da8..97f8518 100644 --- a/src/UseCases/Embed/EmbedUseCase.php +++ b/src/UseCases/Embed/EmbedUseCase.php @@ -80,6 +80,7 @@ private function createRendererConfig( EmbedRequest $request ): RendererConfig { language: $request->language ?? '', showLineNumbers: $request->showLineNumbers ?? false, showSpecificLines: $request->showSpecificLines ?? '', + render: $request->render ?? false, showEditButton: $request->showEditButton ); } diff --git a/tests/Unit/Domain/WikiContentRendererFactoryTest.php b/tests/Unit/Domain/WikiContentRendererFactoryTest.php index 591bb2f..228a6e5 100644 --- a/tests/Unit/Domain/WikiContentRendererFactoryTest.php +++ b/tests/Unit/Domain/WikiContentRendererFactoryTest.php @@ -22,6 +22,7 @@ public function testMarkdownFileAndConfigWithoutLanguageCreatesMarkdownRenderer( language: '', showLineNumbers: false, showSpecificLines: '', + render: true, showEditButton: false ) ) @@ -37,6 +38,7 @@ public function testConfigWithoutLanguageCreatesCodeRenderer(): void { language: '', showLineNumbers: false, showSpecificLines: '', + render: false, showEditButton: false ) ) @@ -52,6 +54,7 @@ public function testConfigWithLanguageCreatesCodeRenderer(): void { language: 'php', showLineNumbers: false, showSpecificLines: '', + render: false, showEditButton: false ) ) @@ -67,6 +70,7 @@ public function testConfigWithLanguageAndLineNumbersCreatesCodeRenderer(): void language: 'php', showLineNumbers: true, showSpecificLines: '', + render: false, showEditButton: false ) ) @@ -82,6 +86,7 @@ public function testConfigWithMarkdownLanguageCreatesCodeRenderer(): void { language: 'md', showLineNumbers: false, showSpecificLines: '', + render: false, showEditButton: false ) ) diff --git a/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php b/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php index fb073af..8e5e20b 100644 --- a/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php +++ b/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php @@ -71,6 +71,7 @@ private function createRequest( string $fileUrl ): EmbedRequest { language: '', showLineNumbers: false, showSpecificLines: '', + render: false, showEditButton: false ); }