phil_s Posted September 15, 2016 Share Posted September 15, 2016 While looking for a way to add target="_blank" to markdown formatted fields I came across these little gems in the TextformatterMarkdownExtra.module (in the wire>modules>textformatter folder, I'm running v3.0.33). Turns out someone (Ryan? Did you add this?) already thought of it: /** * A couple RCD extentions to MarkDown syntax, to be executed after Markdown has already had it's way with the text */ protected function markdownExtensions(&$str) { // add id attribute to a tag, when followed by a #myid if(strpos($str, '>#')) $str = preg_replace('/<([a-z][a-z0-9]*)([^>]*>.*?)(<\/\\1>)#([a-z][-_a-z0-9]*)\b/', '<$1 id="$4"$2$3', $str); // add class attribute to tag when followed by a .myclass if(strpos($str, '>.')) $str = preg_replace('/<([a-z][a-z0-9]*)([^>]*>.*?)(<\/\\1>)\.([a-z][-_a-z0-9]*)\b/', '<$1 class="$4"$2$3', $str); // href links open in new window when followed by a plus sign, i.e. [google](http://google.com)+ if(strpos($str, '</a>+')) $str = preg_replace('/<a ([^>]+>.+?<\/a>)\+/', '<a target="_blank" $1', $str); // strip out comments // if(strpos($text, '/*') !== false) $text = preg_replace('{/\*.*?\*/}s', '', $text); // if(strpos($text, '//') !== false) $text = preg_replace('{^//.*$}m', '', $text); } Adding: $f->addOption(self::flavorRCD, 'RCD extentions'); after line 107 of the file, adds an option field which you can handily check from the module's config screen in the admin. Now, the question I should have asked before doing this is: Does changing that have any detrimental effect on things I don't know about yet? : ) Would be great if someone with actual knowledge on this could chime in before I shoot myself in the foot (There must be a reason why this is not active by default?) Cheers guys! Phil Link to comment Share on other sites More sharing options...
szabesz Posted September 16, 2016 Share Posted September 16, 2016 On 2016-9-15 at 11:26 PM, phil_s said: Would be great if someone with actual knowledge on this could chime in before I shoot myself in the foot (There must be a reason why this is not active by default?) That function is now deprecated. You can create a Textformatter module if you want to, eg.: Spoiler <?php class TextformatterSiteMarkdownAddons extends Textformatter { /** * Return module information. * * @return array Module info */ public static function getModuleInfo() { return array( 'title' => 'Textformatter Site Markdown Addons', 'summary' => 'Adds some extra parsing to Markdown', 'version' => 101 ); } /** * Format the given text string. * * @param Page $page * @param Field $field * @param string $value */ public function formatValue(Page $page, Field $field, &$value) { // href links open in new window when followed by a plus sign, i.e. [google](http://google.com)+ if(strpos($value, '</a>+')) $value = preg_replace('/<a ([^>]+>.+?<\/a>)\+/', '<a target="_blank" $1', $value); } } 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now