Markdown.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. $this->id('mkd-'.Str::random(8));
  30. }
  31. /**
  32. * @param string|Renderable $markdown
  33. *
  34. * @return $this
  35. */
  36. public function content($markdown)
  37. {
  38. $this->content = &$markdown;
  39. return $this;
  40. }
  41. protected function renderContent()
  42. {
  43. return Helper::render($this->content);
  44. }
  45. public function render()
  46. {
  47. $this->addVariables([
  48. 'id' => $this->id(),
  49. 'content' => $this->renderContent(),
  50. ]);
  51. return parent::render();
  52. }
  53. }