TestMdRender.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Api\MdRender;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Support\Facades\Log;
  7. use App\Tools\Markdown;
  8. class TestMdRender extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan test:md.render term --format=unity --driver=str
  13. * @var string
  14. */
  15. protected $signature = 'test:md.render {item?} {--format=html} {--driver=morus}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if (\App\Tools\Tools::isStop()) {
  39. return 0;
  40. }
  41. $content = <<<md
  42. # 测试
  43. ## 测试
  44. {{note|text=这是一个普通信息提示|type=info}}
  45. 下面是一个警告信息:
  46. {{warning|message=请注意这个重要提示|title=重要}}
  47. 你也可以使用位置参数:
  48. {{note|
  49. 成功完成操作|
  50. success}}
  51. 支持嵌套模板:
  52. {{info|
  53. content=外层信息 {{note|内嵌提示|warning}} 继续外层|
  54. title=嵌套示例}}
  55. **粗体文本** 和 *斜体文本* 也被支持。
  56. md;
  57. $parser = new \App\Services\Template\TemplateService(false);
  58. $render = $parser->parseAndRender($content, 'json');
  59. var_dump($render);
  60. return 0;
  61. Log::info('md render start item=' . $this->argument('item'));
  62. $data = array();
  63. $data['bold'] = <<<md
  64. **三十位** 经在[中间]六处为**[licchavi]**,在极果为**慧解脱**
  65. md;
  66. $data['sentence'] = <<<md
  67. {{168-916-2-9}}
  68. md;
  69. $data['link'] = <<<md
  70. aa `[link](wikipali.org/aa.php?view=b&c=d)` bb
  71. md;
  72. $data['term'] = <<<md
  73. ## term
  74. [[bhagavantu]]
  75. md;
  76. $data['noteMulti'] = <<<md
  77. ## heading
  78. [点击](http://127.0.0.1:3000/my/article/para/168-876?mode=edit&channel=00ae2c48-c204-4082-ae79-79ba2740d506&book=168&par=876)
  79. ----
  80. dfef
  81. ```
  82. bla **content**
  83. {{99-556-8-12}}
  84. bla **content**
  85. ```
  86. md;
  87. $data['note'] = '`bla **bold** _em_ bla`';
  88. $data['noteTpl'] = <<<md
  89. {{note|trigger=kacayana|text=bla **bold** _em_ bla}}
  90. md;
  91. $data['noteTpl2'] = <<<md
  92. {{note|trigger=kacayana|text={{99-556-8-12}}}}
  93. md;
  94. $data['trigger'] = <<<md
  95. ## heading
  96. ddd
  97. - title
  98. content-1
  99. - title-2
  100. content-2
  101. aaa bbb
  102. md;
  103. $data['exercise'] = <<<md
  104. {{168-916-10-37}}
  105. {{exercise|1|((168-916-10-37))}}
  106. {{exercise|
  107. id=1|
  108. content={{168-916-10-37}}
  109. }}
  110. {{exercise|
  111. id=2|
  112. content=# ddd}}
  113. md;
  114. $data['article'] = <<<md
  115. {{article|
  116. type=article|
  117. id=27ade9ad-2d0c-4f66-b857-e9335252cc08|
  118. title=第一章 戒律概说(Vinaya)|
  119. style=modal}}
  120. md;
  121. $data['footnote'] = <<<md
  122. # title
  123. content `note content` `note2 content`
  124. md;
  125. $data['paragraph'] = <<<md
  126. # title
  127. content
  128. {{168-916-10-37}}
  129. {{168-916-10-37}}
  130. the end
  131. md;
  132. $data['img'] = <<<md
  133. # title
  134. content
  135. ![aaa](/images/aaa.jpg)
  136. the end
  137. md;
  138. $data['empty'] = '';
  139. Markdown::driver($this->option('driver'));
  140. $format = $this->option('format');
  141. if (empty($format)) {
  142. $formats = ['react', 'unity', 'text', 'tex', 'html', 'simple'];
  143. } else {
  144. $formats = [$format];
  145. }
  146. foreach ($formats as $format) {
  147. $this->info("format:{$format}");
  148. foreach ($data as $key => $value) {
  149. $_item = $this->argument('item');
  150. if (!empty($_item) && $key !== $_item) {
  151. continue;
  152. }
  153. $mdRender = new MdRender([
  154. 'format' => $format,
  155. 'footnote' => true,
  156. 'paragraph' => true,
  157. ]);
  158. $output = $mdRender->convert($value, ['00ae2c48-c204-4082-ae79-79ba2740d506']);
  159. echo $output;
  160. }
  161. }
  162. return 0;
  163. }
  164. }