Markdown.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Support\Str;
  6. class Markdown extends Widget
  7. {
  8. protected $view = 'admin::widgets.markdown';
  9. /**
  10. * @var string|Renderable
  11. */
  12. protected $content;
  13. /**
  14. * 配置.
  15. *
  16. * @var array
  17. */
  18. protected $options = [
  19. 'htmlDecode' => 'style,script,iframe',
  20. 'emoji' => true,
  21. 'taskList' => true,
  22. 'tex' => true,
  23. 'flowChart' => true,
  24. 'sequenceDiagram' => true,
  25. ];
  26. public function __construct($markdown = '')
  27. {
  28. $this->content($markdown);
  29. }
  30. /**
  31. * @param string|Renderable $markdown
  32. *
  33. * @return $this
  34. */
  35. public function content($markdown)
  36. {
  37. $this->content = &$markdown;
  38. return $this;
  39. }
  40. protected function renderContent()
  41. {
  42. return Helper::render($this->content);
  43. }
  44. public function render()
  45. {
  46. $id = 'mkd-'.Str::random(8);
  47. $this->defaultHtmlAttribute('id', $id);
  48. $this->with([
  49. 'id' => $id,
  50. 'content' => $this->renderContent(),
  51. ]);
  52. return parent::render();
  53. }
  54. }