$text ]); if ($response->successful()) { return $response->json('data'); } else { Log::error('morus_restful fail markdown=' . $text); return Str::markdown($text); } } public static function morus($text) { if (isset($GLOBALS['morus_client'])) { $client = $GLOBALS['morus_client']; } else { $host = config('mint.server.rpc.morus.host') . ':' . config('mint.server.rpc.morus.port'); Log::debug('morus host=' . $host); $client = new \Mint\Morus\V1\MarkdownClient($host, [ 'credentials' => \Grpc\ChannelCredentials::createInsecure(), ]); $GLOBALS['morus_client'] = $client; } $request = new \Mint\Morus\V1\MarkdownToHtmlRequest(); $request->setPayload($text); $request->setSanitize(true); list($response, $status) = $client->ToHtml($request)->wait(); if ($status->code !== \Grpc\STATUS_OK) { Log::error("ERROR: " . $status->code . ", " . $status->details); return Str::markdown($text); } return $response->getPayload(); } public static function strdown($text) { $text = str_replace("** ", "**\r\n ", $text); $html = Str::markdown($text); $html = self::replaceSinglePWithSpan($html); return $html; } /** * 如果字符串中只有一对 p 标签,则替换为 span * * @param string $html * @return string */ public static function replaceSinglePWithSpan(string $html): string { preg_match_all('/
]*>.*?<\/p>/is', $html, $matches); if (count($matches[0]) === 1) { return preg_replace( ['/^\s*
]*)>/i', '/<\/p>\s*$/i'], ['', ''], $html ); } return $html; } }