Markdown.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Tools;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Support\Facades\Log;
  5. class Markdown
  6. {
  7. public static function driver($driver){
  8. switch ($driver) {
  9. case 'morus':
  10. $GLOBALS['markdown.driver'] = 'morus';
  11. break;
  12. default:
  13. unset($GLOBALS['markdown.driver']);
  14. break;
  15. }
  16. }
  17. public static function render($text){
  18. if(isset($GLOBALS['markdown.driver'])){
  19. if($GLOBALS['markdown.driver'] === 'morus'){
  20. return Markdown::morus($text);
  21. }else{
  22. return Markdown::strdown($text);
  23. }
  24. }else{
  25. return Markdown::strdown($text);
  26. }
  27. }
  28. public static function morus($text){
  29. $host = config('mint.server.rpc.morus');
  30. Log::debug('morus host='.$host);
  31. $client = new \Mint\Morus\V1\MarkdownClient($host, [
  32. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  33. ]);
  34. $request = new \Mint\Morus\V1\MarkdownToHtmlRequest();
  35. $request->setPayload($text);
  36. $request->setSanitize(true);
  37. list($response, $status) = $client->ToHtml($request)->wait();
  38. if ($status->code !== \Grpc\STATUS_OK) {
  39. Log::error("ERROR: " . $status->code . ", " . $status->details);
  40. return $text;
  41. }
  42. return $response->getPayload();
  43. }
  44. public static function strdown($text){
  45. return Str::markdown($text);
  46. }
  47. }