Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add README documentation to ExtensionPage #3094

Merged
merged 26 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions js/src/admin/components/ExtensionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import LoadingModal from './LoadingModal';
import ExtensionPermissionGrid from './ExtensionPermissionGrid';
import isExtensionEnabled from '../utils/isExtensionEnabled';
import AdminPage from './AdminPage';
import ReadmeModal from './ReadmeModal';

export default class ExtensionPage extends AdminPage {
oninit(vnode) {
Expand Down Expand Up @@ -196,6 +197,22 @@ export default class ExtensionPage extends AdminPage {
}
});

const extension = this.extension;
items.add(
'readme',
Button.component(
{
icon: 'fab fa-readme',
class: 'Readme-link',
imorland marked this conversation as resolved.
Show resolved Hide resolved
onclick() {
app.modal.show(ReadmeModal, { extension });
},
},
app.translator.trans('core.admin.extension.readme.button_label')
),
10
);
imorland marked this conversation as resolved.
Show resolved Hide resolved

return items;
}

Expand Down
50 changes: 50 additions & 0 deletions js/src/admin/components/ReadmeModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import app from '../../admin/app';
import Modal from '../../common/components/Modal';
import LoadingIndicator from '../../common/components/LoadingIndicator';
import Placeholder from '../../common/components/Placeholder';
import ExtensionReadme from '../models/ExtensionReadme';

export default class ReadmeModal extends Modal {
oninit(vnode) {
super.oninit(vnode);

app.store.models['extension-readmes'] = ExtensionReadme;

this.name = this.attrs.extension.id;
this.extName = this.attrs.extension.extra['flarum-extension'].title;

this.loading = true;

this.loadReadme();
}

className() {
return 'ReadmeModal Modal--large';
}

title() {
return app.translator.trans('core.admin.extension.readme.title', {
extName: this.extName,
});
}

content() {
const text = app.translator.trans('core.admin.extension.readme.no_readme');

return (
<div className="container">
{this.loading ? (
<div className="ReadmeModal-loading">{LoadingIndicator.component()}</div>
) : (
<div>{this.readme.content() ? m.trust(this.readme.content()) : Placeholder.component({ text })}</div>
)}
</div>
);
}

async loadReadme() {
this.readme = await app.store.find('extension-readmes', this.name);
this.loading = false;
m.redraw();
}
}
5 changes: 5 additions & 0 deletions js/src/admin/models/ExtensionReadme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Model from '../../common/Model';

export default class ExtensionReadme extends Model {
content = Model.attribute('content');
}
3 changes: 2 additions & 1 deletion less/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
@import "admin/AppearancePage";
@import "admin/MailPage";
@import "admin/NoJs";
@import "admin/UsersListPage.less";
@import "admin/ReadmeModal";
@import "admin/UsersListPage";
17 changes: 17 additions & 0 deletions less/admin/ExtensionPage.less
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,20 @@
display: inline-block;
margin-left: 8px;
}

.Readme-link {
background: none;
border: none;
cursor: pointer;
color: @muted-color;
}

.ReadmeModal {
.Modal-header {
background: @control-bg;
color: @muted-color
}
img {
max-width: 100%;
}
}
5 changes: 5 additions & 0 deletions less/admin/ReadmeModal.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.ReadmeModal {
.Placeholder {
margin-bottom: 40px;
}
}
4 changes: 4 additions & 0 deletions locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ core:
no_settings: This extension has no settings.
open_modal: Open Settings
permissions_title: Permissions
readme:
imorland marked this conversation as resolved.
Show resolved Hide resolved
button_label: README
no_readme: This extension does not appear to have a README file
title: "{extName} documentation"
purge_button: Purge

# These translations are used in the secondary header.
Expand Down
47 changes: 47 additions & 0 deletions src/Api/Controller/ShowExtensionReadmeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Api\Controller;

use Flarum\Api\Serializer\ExtensionReadmeSerializer;
use Flarum\Extension\ExtensionManager;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class ShowExtensionReadmeController extends AbstractShowController
{
/**
* @var ExtensionManager
*/
protected $extensions;

/**
* {@inheritdoc}
*/
public $serializer = ExtensionReadmeSerializer::class;

public function __construct(ExtensionManager $extensions)
{
$this->extensions = $extensions;
}

/**
* {@inheritdoc}
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$extensionName = Arr::get($request->getQueryParams(), 'name');

RequestUtil::getActor($request)->assertAdmin();

return $this->extensions->getExtension($extensionName);
}
}
35 changes: 35 additions & 0 deletions src/Api/Serializer/ExtensionReadmeSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Api\Serializer;

class ExtensionReadmeSerializer extends AbstractSerializer
{
/**
* {@inheritdoc}
*/
protected function getDefaultAttributes($extension)
{
$attributes = [
'content' => $extension->getReadme()
];

return $attributes;
}

public function getId($extension)
{
return $extension->getId();
}

public function getType($extension)
{
return 'extension-readmes';
}
}
7 changes: 7 additions & 0 deletions src/Api/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@
$route->toController(Controller\UninstallExtensionController::class)
);

// Get readme for an extension
$map->get(
'/extension-readmes/{name}',
'extension.readmes.show',
$route->toController(Controller\ShowExtensionReadmeController::class)
);

// Update settings
$map->post(
'/settings',
Expand Down
25 changes: 25 additions & 0 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use s9e\TextFormatter\Bundles\Fatdown;
use Throwable;

/**
Expand Down Expand Up @@ -525,4 +526,28 @@ public function toArray()
'links' => $this->getLinks(),
], $this->composerJson);
}

/**
* Gets the rendered contents of the extension README file as a HTML string.
*
* @return string|null
*/
public function getReadme(): ?string
{
$content = null;

if (file_exists($file = "$this->path/README.md")) {
$content = file_get_contents($file);
} elseif (file_exists($file = "$this->path/README")) {
$content = file_get_contents($file);
}

if ($content) {
$xml = Fatdown::parse($content);

return Fatdown::render($xml);
}

return null;
}
}