becklyn / mobiledoc
基于PHP的mobiledoc格式渲染器。
2.0.2
2022-01-11 08:33 UTC
Requires
- php: ^7.4 || ^8.0
- ext-dom: *
Requires (Dev)
- ext-json: *
- dms/phpunit-arraysubset-asserts: ^0.3.1
- masterminds/html5: ^2.7.5
- symfony/phpunit-bridge: ^6.0.0
Suggests
- masterminds/html5: To automatically preprocess the given HTML to increase compatibility with the native DOM tools when importing HTML.
README
基于PHP的mobiledoc格式渲染器。
渲染Mobiledoc
use Becklyn\Mobiledoc\Extension\ExtensionRegistry; use Becklyn\Mobiledoc\Renderer\MobiledocRenderer; $extensions = new ExtensionRegistry(); $renderer = new MobiledocRenderer($extensions); // returns the rendered document $document = $renderer->render([ "version" => "0.3.1", // ... rest of the mobiledoc document ]); // returns the mobiledoc $document->getMobiledoc(); // returns the HTML $document->getHtml(); (string) $document:
注册扩展
您的扩展必须扩展 RichTextExtensionInterface
。卡片和原子都统一处理,因此在代码中没有分离。对于给定的名称,您只能有一个扩展。
use Becklyn\Mobiledoc\Extension\ExtensionRegistry; use Becklyn\Mobiledoc\Extension\RichTextExtensionInterface; class IframeCard implements RichTextExtensionInterface { /** * @inheritDoc */ public function getName () : string { return "iframe"; } /** * @inheritDoc */ public function render (?string $content, array $payload) : string { return '<iframe src="' . $payload["src"] . '"></iframe>'; } } $extensions = new ExtensionRegistry(); $extensions->registerExtension(new IframeCard());
- 原子通过
$content
参数接收文本内容,卡片将始终接收null
作为内容。 - 缺失的原子将回退为其内容作为纯文本。
- 缺失的卡片将不渲染。