Skip to content

Commit

Permalink
Add config for default Markdown rendering (#42)
Browse files Browse the repository at this point in the history
* WIP RenderMarkdownByDefault

#37

* Added Code, Test and updated Readme

Signed-off-by: Muhammad Yousuf Fazal <[email protected]>

* Fixes

Signed-off-by: Muhammad Yousuf Fazal <[email protected]>

* Fixing the Incorrect Conflict Resolution

Signed-off-by: Muhammad Yousuf Fazal <[email protected]>

* Minor Change

Signed-off-by: Muhammad Yousuf Fazal <[email protected]>

* Another Fix

Signed-off-by: Muhammad Yousuf Fazal <[email protected]>

* Fix

Signed-off-by: Muhammad Yousuf Fazal <[email protected]>

---------

Signed-off-by: Muhammad Yousuf Fazal <[email protected]>
Co-authored-by: Muhammad Yousuf Fazal <[email protected]>
  • Loading branch information
JeroenDeDauw and myousuffazal authored Oct 28, 2023
1 parent 56ef339 commit 4b10405
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 15 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
1 change: 1 addition & 0 deletions src/Domain/ContentRenderer/RendererConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public string $language,
public bool $showLineNumbers,
public string $showSpecificLines,
public bool $render,
public bool $showEditButton
) {
}
Expand Down
20 changes: 5 additions & 15 deletions src/Domain/WikiContentRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

);
}

}
1 change: 1 addition & 0 deletions src/EmbedExtensionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class EmbedExtensionFactory {

public const DEFAULT_CONFIG = [
'wgLanguageCode' => 'en',
'wgExternalContentRenderMarkdownByDefault' => true,
'wgExternalContentDomainWhitelist' => [],
'wgExternalContentFileExtensionWhitelist' => [ 'md' ],
'wgExternalContentEnableEmbedFunction' => true,
Expand Down
1 change: 1 addition & 0 deletions src/UseCases/Embed/EmbedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public ?string $language,
public ?bool $showLineNumbers,
public ?string $showSpecificLines,
public ?bool $render,
public bool $showEditButton
) {
}
Expand Down
7 changes: 7 additions & 0 deletions src/UseCases/Embed/EmbedRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace ProfessionalWiki\ExternalContent\UseCases\Embed;

use MediaWiki\MediaWikiServices;

class EmbedRequestBuilder {

/**
Expand All @@ -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
);
}
Expand Down
1 change: 1 addition & 0 deletions src/UseCases/Embed/EmbedUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Domain/WikiContentRendererFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testMarkdownFileAndConfigWithoutLanguageCreatesMarkdownRenderer(
language: '',
showLineNumbers: false,
showSpecificLines: '',
render: true,
showEditButton: false
)
)
Expand All @@ -37,6 +38,7 @@ public function testConfigWithoutLanguageCreatesCodeRenderer(): void {
language: '',
showLineNumbers: false,
showSpecificLines: '',
render: false,
showEditButton: false
)
)
Expand All @@ -52,6 +54,7 @@ public function testConfigWithLanguageCreatesCodeRenderer(): void {
language: 'php',
showLineNumbers: false,
showSpecificLines: '',
render: false,
showEditButton: false
)
)
Expand All @@ -67,6 +70,7 @@ public function testConfigWithLanguageAndLineNumbersCreatesCodeRenderer(): void
language: 'php',
showLineNumbers: true,
showSpecificLines: '',
render: false,
showEditButton: false
)
)
Expand All @@ -82,6 +86,7 @@ public function testConfigWithMarkdownLanguageCreatesCodeRenderer(): void {
language: 'md',
showLineNumbers: false,
showSpecificLines: '',
render: false,
showEditButton: false
)
)
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/UseCases/Embed/EmbedUseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private function createRequest( string $fileUrl ): EmbedRequest {
language: '',
showLineNumbers: false,
showSpecificLines: '',
render: false,
showEditButton: false
);
}
Expand Down

0 comments on commit 4b10405

Please sign in to comment.