2
0

Markdown.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Tools;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Http;
  6. class Markdown
  7. {
  8. public static function driver($driver){
  9. $GLOBALS['markdown.driver'] = $driver;
  10. }
  11. public static function render($text){
  12. if(isset($GLOBALS['markdown.driver']) &&
  13. $GLOBALS['markdown.driver'] === 'str'){
  14. return Markdown::strdown($text);
  15. }else{
  16. return Markdown::strdown($text);
  17. }
  18. }
  19. public static function morus_restful($text){
  20. $host = config('mint.server.rpc.morus.host');
  21. Log::debug('morus host='.$host);
  22. $response = Http::post($host, [
  23. 'text'=>$text
  24. ]);
  25. if($response->successful()){
  26. return $response->json('data');
  27. }else{
  28. Log::error('morus_restful fail markdown='.$text);
  29. return Str::markdown($text);
  30. }
  31. }
  32. public static function morus($text){
  33. if(isset($GLOBALS['morus_client'])){
  34. $client = $GLOBALS['morus_client'];
  35. }else{
  36. $host = config('mint.server.rpc.morus.host') . ':'. config('mint.server.rpc.morus.port');
  37. Log::debug('morus host='.$host);
  38. $client = new \Mint\Morus\V1\MarkdownClient($host, [
  39. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  40. ]);
  41. $GLOBALS['morus_client'] = $client;
  42. }
  43. $request = new \Mint\Morus\V1\MarkdownToHtmlRequest();
  44. $request->setPayload($text);
  45. $request->setSanitize(true);
  46. list($response, $status) = $client->ToHtml($request)->wait();
  47. if ($status->code !== \Grpc\STATUS_OK) {
  48. Log::error("ERROR: " . $status->code . ", " . $status->details);
  49. return Str::markdown($text);
  50. }
  51. return $response->getPayload();
  52. }
  53. public static function strdown($text){
  54. $text = str_replace("** ","**\r\n ",$text);
  55. return Str::markdown($text);
  56. }
  57. }