1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-13 18:32:41 +01:00
phorge-phorge/src/infrastructure/markup/rule/PhabricatorRemarkupRulePhriction.php
Bob Trahan 5b1d73e5dd make remarkup savvier about fragments in phriction links
Summary:
use PhutilURI class to get the slug and the fragment, normalize the slug, and then glue it back together with another PhutilURI

fixes https://github.com/facebook/phabricator/issues/228

Test Plan: had a few links like [[ example/doc#title | wiki fun ]] and verified links generated were correct

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3894
2012-11-05 15:47:51 -08:00

40 lines
992 B
PHP

<?php
/**
* @group markup
*/
final class PhabricatorRemarkupRulePhriction
extends PhutilRemarkupRule {
public function apply($text) {
return preg_replace_callback(
'@\B\\[\\[([^|\\]]+)(?:\\|([^\\]]+))?\\]\\]\B@U',
array($this, 'markupDocumentLink'),
$text);
}
public function markupDocumentLink($matches) {
$link = trim($matches[1]);
$name = trim(idx($matches, 2, $link));
$name = explode('/', trim($name, '/'));
$name = end($name);
$uri = new PhutilURI($link);
$slug = $uri->getPath();
$fragment = $uri->getFragment();
$slug = PhabricatorSlug::normalize($slug);
$slug = PhrictionDocument::getSlugURI($slug);
$href = (string) id(new PhutilURI($slug))->setFragment($fragment);
return $this->getEngine()->storeText(
phutil_render_tag(
'a',
array(
'href' => $href,
'class' => 'phriction-link',
),
phutil_escape_html($name)));
}
}