2
0

NoteTemplate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Services\Templates;
  3. use App\Http\Api\MdRender;
  4. class NoteTemplate extends AbstractTemplate
  5. {
  6. public function __construct() {}
  7. public function render(): array
  8. {
  9. $note = $this->getParam("text", 1);
  10. $trigger = $this->getParam("trigger", 2);
  11. $props = ["note" => $note];
  12. $innerString = "";
  13. if (!empty($trigger)) {
  14. $props["trigger"] = $trigger;
  15. $innerString = $props["trigger"];
  16. }
  17. if ($this->options['format'] === 'unity') {
  18. $props["note"] = MdRender::render(
  19. $props["note"],
  20. $this->options['channel_id'],
  21. null,
  22. 'read',
  23. 'translation',
  24. 'markdown',
  25. 'unity'
  26. );
  27. }
  28. $output = $note;
  29. switch ($this->options['format']) {
  30. case 'react':
  31. $output = [
  32. 'props' => base64_encode(\json_encode($props)),
  33. 'html' => $innerString,
  34. 'tag' => 'span',
  35. 'tpl' => 'note',
  36. ];
  37. break;
  38. case 'unity':
  39. $output = [
  40. 'props' => base64_encode(\json_encode($props)),
  41. 'tpl' => 'note',
  42. ];
  43. break;
  44. case 'html':
  45. if (isset($GLOBALS['note_sn'])) {
  46. $GLOBALS['note_sn']++;
  47. } else {
  48. $GLOBALS['note_sn'] = 1;
  49. $GLOBALS['note'] = array();
  50. }
  51. $GLOBALS['note'][] = [
  52. 'sn' => $GLOBALS['note_sn'],
  53. 'trigger' => $trigger,
  54. 'content' => MdRender::render(
  55. $props["note"],
  56. $this->options['channel_id'],
  57. null,
  58. 'read',
  59. 'translation',
  60. 'markdown',
  61. 'html'
  62. ),
  63. ];
  64. $link = "<a href='#footnote-" . $GLOBALS['note_sn'] . "' name='note-" . $GLOBALS['note_sn'] . "'>";
  65. if (empty($trigger)) {
  66. $output = $link . "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  67. } else {
  68. $output = $link . $trigger . "</a>";
  69. }
  70. break;
  71. case 'text':
  72. $output = $trigger;
  73. break;
  74. case 'tex':
  75. $output = $trigger;
  76. break;
  77. case 'simple':
  78. $output = '';
  79. break;
  80. case 'markdown':
  81. if (isset($GLOBALS['note_sn'])) {
  82. $GLOBALS['note_sn']++;
  83. } else {
  84. $GLOBALS['note_sn'] = 1;
  85. $GLOBALS['note'] = array();
  86. }
  87. $content = MdRender::render(
  88. $props["note"],
  89. $this->options['channel_id'],
  90. null,
  91. 'read',
  92. 'translation',
  93. 'markdown',
  94. 'markdown'
  95. );
  96. $output = '[^' . $GLOBALS['note_sn'] . ']';
  97. $GLOBALS['note'][] = [
  98. 'sn' => $GLOBALS['note_sn'],
  99. 'trigger' => $trigger,
  100. 'content' => $content,
  101. ];
  102. //$output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
  103. break;
  104. default:
  105. $output = '';
  106. break;
  107. }
  108. return $output;
  109. }
  110. }