CjkFriendlyEmphasisParser.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Services\Markup\Parsing;
  3. use League\CommonMark\Delimiter\Delimiter;
  4. use League\CommonMark\Environment\EnvironmentAwareInterface;
  5. use League\CommonMark\Environment\EnvironmentInterface;
  6. use League\CommonMark\Node\Inline\Text;
  7. use League\CommonMark\Parser\Inline\InlineParserInterface;
  8. use League\CommonMark\Parser\Inline\InlineParserMatch;
  9. use League\CommonMark\Parser\InlineParserContext;
  10. use League\CommonMark\Util\RegexHelper;
  11. /**
  12. * `*` / `**` 强调的分隔符解析,按 markdown-cjk-friendly 提案放宽了「贴合」(flanking)规则。
  13. *
  14. * CommonMark 的规则为空格分词的语言设计:开头的 `**` 后面紧跟标点时,前面必须是空白或标点;
  15. * 结尾的 `**` 前面紧挨标点时,后面必须是空白或标点。中文没有空格,粗体又常常把引号、括号包在里面,
  16. * `从**“tuccha”**的角度` 两头都不成立,`**` 原样露出来。
  17. *
  18. * 放宽:在「紧挨着标点」的那一侧,中日韩字符(汉字、假名、谚文、注音)与空白同等对待。
  19. * 两侧都不是 CJK 字符时,判断与 CommonMark 完全相同。
  20. * 参考:https://github.com/tats-u/markdown-cjk-friendly
  21. *
  22. * 只接管 `*`:`_` 有词内不能强调的规则,中文正文也很少用;`~~` 删除线暂不处理。
  23. * 逻辑照 league/commonmark 的 DelimiterParser(final、@internal,不能继承),只换了判断开闭的那一步;
  24. * 配对与生成强调节点仍由原来的 EmphasisDelimiterProcessor 做。
  25. */
  26. final class CjkFriendlyEmphasisParser implements EnvironmentAwareInterface, InlineParserInterface
  27. {
  28. private const CJK = '/^[\p{Han}\p{Hiragana}\p{Katakana}\p{Hangul}\p{Bopomofo}]/u';
  29. private EnvironmentInterface $environment;
  30. public function setEnvironment(EnvironmentInterface $environment): void
  31. {
  32. $this->environment = $environment;
  33. }
  34. public function getMatchDefinition(): InlineParserMatch
  35. {
  36. return InlineParserMatch::string('*');
  37. }
  38. public function parse(InlineParserContext $inlineContext): bool
  39. {
  40. $processor = $this->environment->getDelimiterProcessors()->getDelimiterProcessor('*');
  41. if ($processor === null) {
  42. return false;
  43. }
  44. $cursor = $inlineContext->getCursor();
  45. $charBefore = $cursor->peek(-1) ?? "\n";
  46. $count = 0;
  47. while ($cursor->peek($count) === '*') {
  48. $count++;
  49. }
  50. if ($count < $processor->getMinLength()) {
  51. return false;
  52. }
  53. $cursor->advanceBy($count);
  54. $charAfter = $cursor->getCurrentCharacter() ?? "\n";
  55. [$canOpen, $canClose] = self::flanking($charBefore, $charAfter);
  56. $canOpen = $canOpen && $processor->getOpeningCharacter() === '*';
  57. $canClose = $canClose && $processor->getClosingCharacter() === '*';
  58. if (! $canOpen && ! $canClose) {
  59. $inlineContext->getContainer()->appendChild(new Text(str_repeat('*', $count)));
  60. return true;
  61. }
  62. $node = new Text(str_repeat('*', $count), ['delim' => true]);
  63. $inlineContext->getContainer()->appendChild($node);
  64. $inlineContext->getDelimiterStack()->push(
  65. new Delimiter('*', $count, $node, $canOpen, $canClose, $cursor->getPosition())
  66. );
  67. return true;
  68. }
  69. /**
  70. * 左贴合(能开)与右贴合(能闭)。与 CommonMark 的区别只在各自第二个条件里多了「或者另一侧是 CJK 字符」。
  71. *
  72. * @return array{0: bool, 1: bool}
  73. */
  74. public static function flanking(string $before, string $after): array
  75. {
  76. $afterSpace = preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $after) === 1;
  77. $afterPunct = preg_match(RegexHelper::REGEX_PUNCTUATION, $after) === 1;
  78. $beforeSpace = preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $before) === 1;
  79. $beforePunct = preg_match(RegexHelper::REGEX_PUNCTUATION, $before) === 1;
  80. $beforeCjk = preg_match(self::CJK, $before) === 1;
  81. $afterCjk = preg_match(self::CJK, $after) === 1;
  82. $left = ! $afterSpace && (! $afterPunct || $beforeSpace || $beforePunct || $beforeCjk);
  83. $right = ! $beforeSpace && (! $beforePunct || $afterSpace || $afterPunct || $afterCjk);
  84. return [$left, $right];
  85. }
  86. }