MdRender.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <?php
  2. namespace App\Http\Api;
  3. use Illuminate\Support\Str;
  4. use mustache\mustache;
  5. use App\Models\DhammaTerm;
  6. use App\Models\PaliText;
  7. use App\Models\Channel;
  8. use App\Http\Controllers\CorpusController;
  9. use Illuminate\Support\Facades\Cache;
  10. use App\Tools\RedisClusters;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Tools\Markdown;
  13. define("STACK_DEEP", 8);
  14. class MdRender
  15. {
  16. /**
  17. * 文字渲染模式
  18. * read 阅读模式
  19. * edit 编辑模式
  20. */
  21. protected $options = [
  22. 'mode' => 'read',
  23. 'channelType' => 'translation',
  24. 'contentType' => "markdown",
  25. 'format' => 'react',
  26. 'debug' => [],
  27. 'studioId' => null,
  28. 'lang' => 'zh-Hans',
  29. 'footnote' => false,
  30. 'paragraph' => false,
  31. ];
  32. public function __construct($options = [])
  33. {
  34. foreach ($options as $key => $value) {
  35. $this->options[$key] = $value;
  36. }
  37. }
  38. /**
  39. * 将句子模版组成的段落复制一份,为了实现巴汉逐段对读
  40. */
  41. private function preprocessingForParagraph($input)
  42. {
  43. if (!$this->options['paragraph']) {
  44. return $input;
  45. }
  46. $paragraphs = explode("\n\n", $input);
  47. $output = [];
  48. foreach ($paragraphs as $key => $paragraph) {
  49. # 判断是否是纯粹的句子模版
  50. $pattern = "/\{\{sent\|id=([0-9].+?)\}\}/";
  51. $replacement = '';
  52. $space = preg_replace($pattern, $replacement, $paragraph);
  53. $space = str_replace('>', '', $space);
  54. if (empty(trim($space))) {
  55. $output[] = str_replace('}}', '|text=origin}}', $paragraph);
  56. $output[] = str_replace('}}', '|text=translation}}', $paragraph);
  57. } else {
  58. $output[] = $paragraph;
  59. }
  60. }
  61. return implode("\n\n", $output);
  62. }
  63. /**
  64. * 按照{{}}把字符串切分成三个部分。模版之前的,模版,和模版之后的
  65. */
  66. private function tplSplit($tpl)
  67. {
  68. $before = strpos($tpl, '{{');
  69. if ($before === FALSE) {
  70. //未找到
  71. return ['data' => [$tpl, '', ''], 'error' => 0];
  72. } else {
  73. $pointer = $before;
  74. $stack = array();
  75. $stack[] = $pointer;
  76. $after = substr($tpl, $pointer + 2);
  77. while (!empty($after) && count($stack) > 0 && count($stack) < STACK_DEEP) {
  78. $nextBegin = strpos($after, "{{");
  79. $nextEnd = strpos($after, "}}");
  80. if ($nextBegin !== FALSE) {
  81. if ($nextBegin < $nextEnd) {
  82. //有嵌套找到最后一个}}
  83. $pointer = $pointer + 2 + $nextBegin;
  84. $stack[] = $pointer;
  85. $after = substr($tpl, $pointer + 2);
  86. } else if ($nextEnd !== FALSE) {
  87. //无嵌套有结束
  88. $pointer = $pointer + 2 + $nextEnd;
  89. array_pop($stack);
  90. $after = substr($tpl, $pointer + 2);
  91. } else {
  92. //无结束符 没找到
  93. break;
  94. }
  95. } else if ($nextEnd !== FALSE) {
  96. $pointer = $pointer + 2 + $nextEnd;
  97. array_pop($stack);
  98. $after = substr($tpl, $pointer + 2);
  99. } else {
  100. //没找到
  101. break;
  102. }
  103. }
  104. if (count($stack) > 0) {
  105. if (count($stack) === STACK_DEEP) {
  106. return ['data' => [$tpl, '', ''], 'error' => 2];
  107. } else {
  108. //未关闭
  109. return ['data' => [$tpl, '', ''], 'error' => 1];
  110. }
  111. } else {
  112. return [
  113. 'data' =>
  114. [
  115. substr($tpl, 0, $before),
  116. substr($tpl, $before, $pointer - $before + 2),
  117. substr($tpl, $pointer + 2)
  118. ],
  119. 'error' => 0
  120. ];
  121. }
  122. }
  123. }
  124. private function wiki2xml(string $wiki, $channelId = []): string
  125. {
  126. /**
  127. * 渲染markdown里面的模版
  128. */
  129. $remain = $wiki;
  130. $buffer = array();
  131. do {
  132. $arrWiki = $this->tplSplit($remain);
  133. $buffer[] = $arrWiki['data'][0];
  134. $tpl = $arrWiki['data'][1];
  135. if (!empty($tpl)) {
  136. /**
  137. * 处理模版 提取参数
  138. */
  139. $tpl = str_replace("|\n", "|", $tpl);
  140. $pattern = "/\{\{(.+?)\|/";
  141. $replacement = '<MdTpl class="tpl" name="$1"><param>';
  142. $tpl = preg_replace($pattern, $replacement, $tpl);
  143. $tpl = str_replace("}}", "</param></MdTpl>", $tpl);
  144. $tpl = str_replace("|", "</param><param>", $tpl);
  145. /**
  146. * 替换变量名
  147. */
  148. $pattern = "/<param>([a-z]+?)=/";
  149. $replacement = '<param name="$1">';
  150. $tpl = preg_replace($pattern, $replacement, $tpl);
  151. //tpl to react
  152. $tpl = str_replace('<param', '<span class="param"', $tpl);
  153. $tpl = str_replace('</param>', '</span>', $tpl);
  154. $tpl = $this->xml2tpl($tpl, $channelId);
  155. $buffer[] = $tpl;
  156. }
  157. $remain = $arrWiki['data'][2];
  158. } while (!empty($remain));
  159. $html = implode('', $buffer);
  160. return $html;
  161. }
  162. private function xmlQueryId(string $xml, string $id): string
  163. {
  164. try {
  165. $dom = simplexml_load_string($xml);
  166. } catch (\Exception $e) {
  167. Log::error($e);
  168. return "<div></div>";
  169. }
  170. $tpl_list = $dom->xpath('//MdTpl');
  171. foreach ($tpl_list as $key => $tpl) {
  172. foreach ($tpl->children() as $param) {
  173. # 处理每个参数
  174. if ($param->getName() === "param") {
  175. foreach ($param->attributes() as $pa => $pa_value) {
  176. $pValue = $pa_value->__toString();
  177. if ($pa === "name" && $pValue === "id") {
  178. if ($param->__toString() === $id) {
  179. return $tpl->asXML();
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return "<div></div>";
  187. }
  188. public static function take_sentence(string $xml): array
  189. {
  190. $output = [];
  191. try {
  192. $dom = simplexml_load_string($xml);
  193. } catch (\Exception $e) {
  194. Log::error($e);
  195. return $output;
  196. }
  197. $tpl_list = $dom->xpath('//MdTpl');
  198. foreach ($tpl_list as $key => $tpl) {
  199. foreach ($tpl->attributes() as $a => $a_value) {
  200. if ($a === "name") {
  201. if ($a_value->__toString() === "sent") {
  202. foreach ($tpl->children() as $param) {
  203. # 处理每个参数
  204. if ($param->getName() === "param") {
  205. $sent = $param->__toString();
  206. if (!empty($sent)) {
  207. $output[] = $sent;
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. return $output;
  217. }
  218. private function xml2tpl(string $xml, $channelId = []): string
  219. {
  220. /**
  221. * 解析xml
  222. * 获取模版参数
  223. * 生成react 组件参数
  224. */
  225. try {
  226. //$dom = simplexml_load_string($xml);
  227. $doc = new \DOMDocument();
  228. $xml = str_replace('MdTpl', 'dfn', $xml);
  229. $xml = mb_convert_encoding($xml, 'HTML-ENTITIES', "UTF-8");
  230. $ok = $doc->loadHTML($xml, LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  231. } catch (\Exception $e) {
  232. Log::error($e);
  233. Log::error($xml);
  234. return "<span>xml解析错误{$e}</span>";
  235. }
  236. if (!$ok) {
  237. return "<span>xml解析错误</span>";
  238. }
  239. /*
  240. if(!$dom){
  241. Log::error($xml);
  242. return "<span>xml解析错误</span>";
  243. }
  244. */
  245. $tpl_list = $doc->getElementsByTagName('dfn');
  246. foreach ($tpl_list as $key => $tpl) {
  247. /**
  248. * 遍历 MdTpl 处理参数
  249. */
  250. $props = [];
  251. $tpl_name = '';
  252. foreach ($tpl->attributes as $a => $a_value) {
  253. if ($a_value->nodeName === "name") {
  254. $tpl_name = $a_value->nodeValue;
  255. break;
  256. }
  257. }
  258. $param_id = 0;
  259. $child = $tpl->firstChild;
  260. while ($child) {
  261. # 处理每个参数
  262. if ($child->nodeName === "span") {
  263. $param_id++;
  264. $paramName = "";
  265. foreach ($child->attributes as $pa => $pa_value) {
  266. if ($pa_value->nodeName === "name") {
  267. $nodeText = $pa_value->nodeValue;
  268. $props["{$nodeText}"] = $child->nodeValue;
  269. $paramName = $pa_value;
  270. }
  271. }
  272. if (empty($paramName)) {
  273. foreach ($child->childNodes as $param_child) {
  274. # code...
  275. if ($param_child->nodeType === 3) {
  276. $props["{$param_id}"] = $param_child->nodeValue;
  277. }
  278. }
  279. }
  280. }
  281. $child = $child->nextSibling;
  282. }
  283. /**
  284. * 生成模版参数
  285. *
  286. */
  287. //TODO 判断$channelId里面的是否都是uuid
  288. $channelInfo = [];
  289. foreach ($channelId as $key => $id) {
  290. $channelInfo[] = Channel::where('uid', $id)->first();
  291. }
  292. $tplRender = new TemplateRender(
  293. $props,
  294. $channelInfo,
  295. $this->options['mode'],
  296. $this->options['format'],
  297. $this->options['studioId'],
  298. $this->options['debug'],
  299. $this->options['lang'],
  300. );
  301. $tplRender->options($this->options);
  302. $tplProps = $tplRender->render($tpl_name);
  303. if ($this->options['format'] === 'react' && $tplProps) {
  304. $props = $doc->createAttribute("props");
  305. $props->nodeValue = $tplProps['props'];
  306. $tpl->appendChild($props);
  307. $attTpl = $doc->createAttribute("tpl");
  308. $attTpl->nodeValue = $tplProps['tpl'];
  309. $tpl->appendChild($attTpl);
  310. $htmlElement = $doc->createElement($tplProps['tag']);
  311. $htmlElement->nodeValue = $tplProps['html'];
  312. $tpl->appendChild($htmlElement);
  313. }
  314. }
  315. $html = $doc->saveHTML();
  316. $html = str_replace(['<dfn', '</dfn>'], ['<MdTpl', '</MdTpl>'], $html);
  317. switch ($this->options['format']) {
  318. case 'react':
  319. return trim($html);
  320. break;
  321. case 'unity':
  322. if ($tplProps) {
  323. return "{{" . "{$tplProps['tpl']}|{$tplProps['props']}" . "}}";
  324. } else {
  325. return '';
  326. }
  327. break;
  328. case 'html':
  329. if (isset($tplProps)) {
  330. if (is_array($tplProps)) {
  331. return '';
  332. } else {
  333. return $tplProps;
  334. }
  335. } else {
  336. Log::error('tplProps undefine');
  337. return '';
  338. }
  339. break;
  340. case 'tex':
  341. if (isset($tplProps)) {
  342. if (is_array($tplProps)) {
  343. return '';
  344. } else {
  345. return $tplProps;
  346. }
  347. } else {
  348. Log::error('tplProps undefine');
  349. return '';
  350. }
  351. break;
  352. default:
  353. /**text simple markdown */
  354. if (isset($tplProps)) {
  355. if (is_array($tplProps)) {
  356. return '';
  357. } else {
  358. return $tplProps;
  359. }
  360. } else {
  361. Log::error('tplProps undefine');
  362. return '';
  363. }
  364. break;
  365. }
  366. }
  367. /**
  368. * 将markdown文件中的模版转换为标准的wiki模版
  369. */
  370. private function markdown2wiki(string $markdown): string
  371. {
  372. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  373. $markdown = iconv('UTF-8', 'UTF-8//IGNORE', $markdown);
  374. /**
  375. * nissaya
  376. * aaa=bbb\n
  377. * {{nissaya|aaa|bbb}}
  378. */
  379. if ($this->options['channelType'] === 'nissaya') {
  380. if ($this->options['contentType'] === "json") {
  381. $json = json_decode($markdown);
  382. $nissayaWord = [];
  383. if (is_array($json)) {
  384. foreach ($json as $word) {
  385. if (count($word->sn) === 1) {
  386. //只输出第一层级
  387. $str = "{{nissaya|";
  388. if (isset($word->word->value)) {
  389. $str .= $word->word->value;
  390. }
  391. $str .= "|";
  392. if (isset($word->meaning->value)) {
  393. $str .= $word->meaning->value;
  394. }
  395. $str .= "}}";
  396. $nissayaWord[] = $str;
  397. }
  398. }
  399. } else {
  400. Log::error('json data is not array', ['data' => $markdown]);
  401. }
  402. $markdown = implode('', $nissayaWord);
  403. } else if ($this->options['contentType'] === "markdown") {
  404. $lines = explode("\n", $markdown);
  405. $newLines = array();
  406. foreach ($lines as $line) {
  407. if (strstr($line, '=') === FALSE) {
  408. $newLines[] = $line;
  409. } else {
  410. $nissaya = explode('=', $line);
  411. $meaning = array_slice($nissaya, 1);
  412. $meaning = implode('=', $meaning);
  413. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  414. }
  415. }
  416. $markdown = implode("\n", $newLines);
  417. }
  418. }
  419. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  420. /**
  421. * 处理 mermaid
  422. */
  423. if (strpos($markdown, "```mermaid") !== false) {
  424. $lines = explode("\n", $markdown);
  425. $newLines = array();
  426. $mermaidBegin = false;
  427. $mermaidString = array();
  428. foreach ($lines as $line) {
  429. if ($line === "```mermaid") {
  430. $mermaidBegin = true;
  431. $mermaidString = [];
  432. continue;
  433. }
  434. if ($mermaidBegin) {
  435. if ($line === "```") {
  436. $newLines[] = "{{mermaid|" . base64_encode(\json_encode($mermaidString)) . "}}";
  437. $mermaidBegin = false;
  438. } else {
  439. $mermaidString[] = $line;
  440. }
  441. } else {
  442. $newLines[] = $line;
  443. }
  444. }
  445. $markdown = implode("\n", $newLines);
  446. }
  447. /**
  448. * 替换换行符
  449. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  450. */
  451. //$markdown = str_replace('<br>','<div></div>',$markdown);
  452. /**
  453. * markdown -> html
  454. */
  455. /*
  456. $html = MdRender::fixHtml($html);
  457. */
  458. #替换术语
  459. $pattern = "/\[\[(.+?)\]\]/";
  460. $replacement = '{{term|$1}}';
  461. $markdown = preg_replace($pattern, $replacement, $markdown);
  462. #替换句子模版
  463. $pattern = "/\{\{([0-9].+?)\}\}/";
  464. $replacement = '{{sent|id=$1}}';
  465. $markdown = preg_replace($pattern, $replacement, $markdown);
  466. /**
  467. * 替换多行注释
  468. * ```
  469. * bla
  470. * bla
  471. * ```
  472. * {{note|
  473. * bla
  474. * bla
  475. * }}
  476. */
  477. if (strpos($markdown, "```\n") !== false) {
  478. $lines = explode("\n", $markdown);
  479. $newLines = array();
  480. $noteBegin = false;
  481. $noteString = array();
  482. foreach ($lines as $line) {
  483. if ($noteBegin) {
  484. if ($line === "```") {
  485. $newLines[] = "}}";
  486. $noteBegin = false;
  487. } else {
  488. $newLines[] = $line;
  489. }
  490. } else {
  491. if ($line === "```") {
  492. $noteBegin = true;
  493. $newLines[] = "{{note|";
  494. continue;
  495. } else {
  496. $newLines[] = $line;
  497. }
  498. }
  499. }
  500. if ($noteBegin) {
  501. $newLines[] = "}}";
  502. }
  503. $markdown = implode("\n", $newLines);
  504. }
  505. /**
  506. * 替换单行注释
  507. * `bla bla`
  508. * {{note|bla}}
  509. */
  510. $pattern = "/`(.+?)`/";
  511. $replacement = '{{note|$1}}';
  512. $markdown = preg_replace($pattern, $replacement, $markdown);
  513. return $markdown;
  514. }
  515. private function markdownToHtml($markdown)
  516. {
  517. $markdown = str_replace('MdTpl', 'mdtpl', $markdown);
  518. $markdown = str_replace(['<param', '</param>'], ['<span', '</span>'], $markdown);
  519. $html = Markdown::render($markdown);
  520. if ($this->options['format'] === 'react') {
  521. $html = $this->fixHtml($html);
  522. }
  523. $html = str_replace('<hr>', '<hr />', $html);
  524. //给H1-6 添加uuid
  525. for ($i = 1; $i < 7; $i++) {
  526. if (strpos($html, "<h{$i}>") === false) {
  527. continue;
  528. }
  529. $output = array();
  530. $input = $html;
  531. $hPos = strpos($input, "<h{$i}>");
  532. while ($hPos !== false) {
  533. $output[] = substr($input, 0, $hPos);
  534. $output[] = "<h{$i} id='" . Str::uuid() . "'>";
  535. $input = substr($input, $hPos + 4);
  536. $hPos = strpos($input, "<h{$i}>");
  537. }
  538. $output[] = $input;
  539. $html = implode('', $output);
  540. }
  541. $html = str_replace('mdtpl', 'MdTpl', $html);
  542. return $html;
  543. }
  544. private function fixHtml($html)
  545. {
  546. $doc = new \DOMDocument();
  547. libxml_use_internal_errors(true);
  548. $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  549. $doc->loadHTML('<span>' . $html . '</span>', LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  550. $fixed = $doc->saveHTML();
  551. $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
  552. return $fixed;
  553. }
  554. public static function init()
  555. {
  556. $GLOBALS["MdRenderStack"] = 0;
  557. }
  558. public function convert($markdown, $channelId = [], $queryId = null)
  559. {
  560. if (isset($GLOBALS["MdRenderStack"]) && is_numeric($GLOBALS["MdRenderStack"])) {
  561. $GLOBALS["MdRenderStack"]++;
  562. } else {
  563. $GLOBALS["MdRenderStack"] = 1;
  564. }
  565. if ($GLOBALS["MdRenderStack"] < 3) {
  566. $output = $this->_convert($markdown, $channelId, $queryId);
  567. } else {
  568. $output = $markdown;
  569. }
  570. $GLOBALS["MdRenderStack"]--;
  571. return $output;
  572. }
  573. private function _convert($markdown, $channelId = [], $queryId = null)
  574. {
  575. if (empty($markdown)) {
  576. switch ($this->options['format']) {
  577. case 'react':
  578. return "<span></span>";
  579. break;
  580. default:
  581. return "";
  582. break;
  583. }
  584. }
  585. $wiki = $this->markdown2wiki($markdown);
  586. $wiki = $this->preprocessingForParagraph($wiki);
  587. $markdownWithTpl = $this->wiki2xml($wiki, $channelId);
  588. if (!is_null($queryId)) {
  589. $html = $this->xmlQueryId($markdownWithTpl, $queryId);
  590. }
  591. $html = $this->markdownToHtml($markdownWithTpl);
  592. //后期处理
  593. $output = '';
  594. switch ($this->options['format']) {
  595. case 'react':
  596. //生成可展开组件
  597. $html = str_replace("<div/>", "<div></div>", $html);
  598. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  599. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  600. $output = preg_replace($pattern, $replacement, $html);
  601. break;
  602. case 'text':
  603. case 'simple':
  604. case 'prompt':
  605. $html = strip_tags($html);
  606. $output = htmlspecialchars_decode($html, ENT_QUOTES);
  607. //$output = html_entity_decode($html);
  608. break;
  609. case 'tex':
  610. $html = strip_tags($html);
  611. $output = htmlspecialchars_decode($html, ENT_QUOTES);
  612. //$output = html_entity_decode($html);
  613. break;
  614. case 'unity':
  615. $html = str_replace(['<strong>', '</strong>', '<em>', '</em>'], ['[%b%]', '[%/b%]', '[%i%]', '[%/i%]'], $html);
  616. $html = strip_tags($html);
  617. $html = str_replace(['[%b%]', '[%/b%]', '[%i%]', '[%/i%]'], ['<b>', '</b>', '<i>', '</i>'], $html);
  618. $output = htmlspecialchars_decode($html, ENT_QUOTES);
  619. break;
  620. case 'html':
  621. $output = htmlspecialchars_decode($html, ENT_QUOTES);
  622. //处理脚注
  623. if ($this->options['footnote'] && isset($GLOBALS['note']) && count($GLOBALS['note']) > 0) {
  624. $output .= '<div><h1>endnote</h1>';
  625. foreach ($GLOBALS['note'] as $footnote) {
  626. $output .= '<p><a name="footnote-' . $footnote['sn'] . '">[' . $footnote['sn'] . ']</a> ' . $footnote['content'] . '</p>';
  627. }
  628. $output .= '</div>';
  629. unset($GLOBALS['note']);
  630. }
  631. //处理图片链接
  632. $output = str_replace('<img src="', '<img src="' . config('app.url'), $output);
  633. break;
  634. case 'markdown':
  635. //处理脚注
  636. $footnotes = array();
  637. if ($this->options['footnote'] && isset($GLOBALS['note']) && count($GLOBALS['note']) > 0) {
  638. foreach ($GLOBALS['note'] as $footnote) {
  639. $footnotes[] = '[^' . $footnote['sn'] . ']: ' . $footnote['content'];
  640. }
  641. unset($GLOBALS['note']);
  642. }
  643. //处理图片链接
  644. $output = str_replace('/attachments/', config('app.url') . "/attachments/", $markdownWithTpl);
  645. $output = $output . "\n\n" . implode("\n\n", $footnotes);
  646. break;
  647. }
  648. return $output;
  649. }
  650. /**
  651. * string[] $channelId
  652. */
  653. public static function render($markdown, $channelId, $queryId = null, $mode = 'read', $channelType = 'translation', $contentType = "markdown", $format = 'react')
  654. {
  655. $mdRender = new MdRender(
  656. [
  657. 'mode' => $mode,
  658. 'channelType' => $channelType,
  659. 'contentType' => $contentType,
  660. 'format' => $format
  661. ]
  662. );
  663. $output = $mdRender->convert($markdown, $channelId, $queryId);
  664. return $output;
  665. }
  666. }