Path: blob/master/src/infrastructure/markup/markuprule/PhutilRemarkupAnchorRule.php
12241 views
<?php12final class PhutilRemarkupAnchorRule extends PhutilRemarkupRule {34public function getPriority() {5return 200.0;6}78public function apply($text) {9return preg_replace_callback(10'/{anchor\s+#([^\s}]+)}/s',11array($this, 'markupAnchor'),12$text);13}1415protected function markupAnchor(array $matches) {16$engine = $this->getEngine();1718if ($engine->isTextMode()) {19return null;20}2122if ($engine->isHTMLMailMode()) {23return null;24}2526if ($engine->isAnchorMode()) {27return null;28}2930if (!$this->isFlatText($matches[0])) {31return $matches[0];32}3334if (!self::isValidAnchorName($matches[1])) {35return $matches[0];36}3738$tag_view = phutil_tag(39'a',40array(41'name' => $matches[1],42),43'');4445return $this->getEngine()->storeText($tag_view);46}4748public static function isValidAnchorName($anchor_name) {49$normal_anchor = self::normalizeAnchor($anchor_name);5051if ($normal_anchor === $anchor_name) {52return true;53}5455return false;56}5758public static function normalizeAnchor($anchor) {59// Replace all latin characters which are not "a-z" or "0-9" with "-".60// Preserve other characters, since non-latin letters and emoji work61// fine in anchors.62$anchor = preg_replace('/[\x00-\x2F\x3A-\x60\x7B-\x7F]+/', '-', $anchor);63$anchor = trim($anchor, '-');6465return $anchor;66}6768}697071