Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

How do I escape @username in a discussion

Am trying to share a LESS CSS code in my forum. When I add the LESS variable syntax which looks like this @less-var, the variable get linked as if it were someone's username. Please how do I stop this from happening?

Comments

  • Options
    jackmaessenjackmaessen ✭✭✭
    edited November 2015

    Actually, you should treat this as a piece of code, which it is. And for code, you better use a highlighter which will display it like this: @less-var

  • Options

    I actually wrapped it in a code tag, and am using googlecodeprettify.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    What formatter are you using?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options

    @hgtonight said:
    What formatter are you using?

    I don't understand what you mean.
    But I know am using a customMention and here is the code:

    class CustomMentions {
    
        public function FormatMentions($Mixed) {
            
            // Handle @mentions.
            if(C('Garden.Format.Mentions')) {
                $Mixed = preg_replace(
                    '/(^|[\s,\.>])@(\w{1,50})\b/i', //{3,20}
                    '\1'.Anchor('@\2', '/@\\2'), $Mixed
                );
            }
    
            // Handle #hashtag searches
            if(C('Garden.Format.Hashtags')) {
                $Mixed = preg_replace(
                    '/(^|[\s,\.>])\#([\w\-]+)(?=[\s,\.!?]|$)/i',
                    '\1'.Anchor('#\2', '/search?Search=%23\2&Mode=like').'\3',
                    $Mixed
                );
            }
    
            // Handle "/me does x" action statements
            if(C('Garden.Format.MeActions')) {
                $Mixed = preg_replace(
                    '/(^|[\n])(\/me)(\s[^(\n)]+)/i',
                    '\1'.Wrap(Wrap('\2', 'span', array('class' => 'MeActionName')).'\3', 'span', array('class' => 'AuthorAction')),
                    $Mixed
                );
            }
            return $Mixed;
        }
    }
    
  • Options
    hgtonighthgtonight ∞ · New Moderator

    Does removing your custom mentions formatter fix this issue?

    If so, we know where to look.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    peregrineperegrine MVP
    edited November 2015

    what editor plugin are you using. buttonbar wysiwyg, etc?

    what inputformatter?

    what version of vanilla are you using?

    If I am not mistaken, it seems to me the code tags do not properly work in vanilla 2.1 to filter out @sign links. by default with html or markdown.

    it is fixed in 2.2rc1 with the protectcodeblocks function and perhaps other fixes as well.

         $Mixed = Gdn_Format::replaceButProtectCodeBlocks(
                            '/(^|[\s,\.>\(])@('.$Pattern.'{1,64})\b/i', //{3,20}
                            '\1'.anchor('@$2', $urlFormat),
                            $Mixed
                        );
    

    calls

        public static function replaceButProtectCodeBlocks($Search, $Replace, $Subject, $IsCallback = false) {
                // Take the code blocks out, replace with a hash of the string, and
                // keep track of what substring got replaced with what hash.
                $CodeBlockContents = array();
                $CodeBlockHashes = array();
                $Subject = preg_replace_callback(
                    '/<code>.*?<\/code>/i',
                    function ($Matches) use (&$CodeBlockContents, &$CodeBlockHashes) {
                        // Surrounded by whitespace to try to prevent the characters
                        // from being picked up by $Pattern.
                        $ReplacementString = ' '.sha1($Matches[0]).' ';
                        $CodeBlockContents[] = $Matches[0];
                        $CodeBlockHashes[] = $ReplacementString;
                        return $ReplacementString;
                    },
                    $Subject
                );
                // Do the requested replacement.
                if ($IsCallback) {
                    $Subject = preg_replace_callback($Search, $Replace, $Subject);
                } else {
                    $Subject = preg_replace($Search, $Replace, $Subject);
                }
                // Put back the code blocks.
                $Subject = str_replace($CodeBlockHashes, $CodeBlockContents, $Subject);
                return $Subject;
            }
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    @hgtonight said:
    Does removing your custom mentions formatter fix this issue?

    No!

  • Options

    @peregrine said:
    what editor plugin are you using. buttonbar wysiwyg, etc?

    buttonbar.

    what inputformatter?

    html

    what version of vanilla are you using?

    2.1.13

  • Options
    peregrineperegrine MVP
    edited November 2015

    you might wait until 2.2 is officially released. It seems to be fixed.

    or if you want to retrieve interim imminent 2.2 version from github you could experiment with that.

    but see the release notes and the readme and backup your database before installing, if you do.

    regarding 2.1.13p1 - I could be wrong, but I think @mentions in code are links and not properly formatted.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    xm1xm1 New
    edited November 2015

    @peregrine can I incorporate the code in my customMention class like So:

    class CustomMentions {
    
        public function FormatMentions($Mixed) {
            
            // Handle @mentions.
            if(C('Garden.Format.Mentions')) {
                $Mixed = self::replaceButProtectCodeBlocks(
                    '/(^|[\s,\.>\(])@(\w{1,64})\b/i', //{3,20}
                    '\1'.anchor('@$2', '/@\\2'),
                    $Mixed
                );
            }
    
            // Handle #hashtag searches
            if(C('Garden.Format.Hashtags')) {
                $Mixed = preg_replace(
                    '/(^|[\s,\.>])\#([\w\-]+)(?=[\s,\.!?]|$)/i',
                    '\1'.Anchor('#\2', '/search?Search=%23\2&Mode=like').'\3',
                    $Mixed
                );
            }
    
            // Handle "/me does x" action statements
            if(C('Garden.Format.MeActions')) {
                $Mixed = preg_replace(
                    '/(^|[\n])(\/me)(\s[^(\n)]+)/i',
                    '\1'.Wrap(Wrap('\2', 'span', array('class' => 'MeActionName')).'\3', 'span', array('class' => 'AuthorAction')),
                    $Mixed
                );
            }
            return $Mixed;
        }
    
        public static function replaceButProtectCodeBlocks($Search, $Replace, $Subject, $IsCallback = false) {
            // Take the code blocks out, replace with a hash of the string, and
            // keep track of what substring got replaced with what hash.
            $CodeBlockContents = array();
            $CodeBlockHashes = array();
            $Subject = preg_replace_callback(
                '/.*?<\/code>/i',
                function ($Matches) use (&$CodeBlockContents, &$CodeBlockHashes) {
                    // Surrounded by whitespace to try to prevent the characters
                    // from being picked up by $Pattern.
                    $ReplacementString = ' '.sha1($Matches[0]).' ';
                    $CodeBlockContents[] = $Matches[0];
                    $CodeBlockHashes[] = $ReplacementString;
                    return $ReplacementString;
                },
                $Subject
            );
            // Do the requested replacement.
            if ($IsCallback) {
                $Subject = preg_replace_callback($Search, $Replace, $Subject);
            } else {
                $Subject = preg_replace($Search, $Replace, $Subject);
            }
            // Put back the code blocks.
            $Subject = str_replace($CodeBlockHashes, $CodeBlockContents, $Subject);
            return $Subject;
        }
    }
    
  • Options

    you can certainly add the code, and see if works, but there may be other things that are changed as well to effect the change besides the protect code blocks,

    I figure since 2.2 is relatively imminent - it might be worth the wait, instead of beating brains out to try to fix.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    Incorporating the code didn't fix the issue.
    Am just going to wait for 2.2.

  • Options
    peregrineperegrine MVP
    edited November 2015

    @xm1

    If you want to turn off mentions completely whether @ is in code blocks or not
    (you may not want to do, but you can if you are not aware).

    you can add this to conf/config.php

    $Configuration['Garden']['Format']['Mentions'] = FALSE; // no linking for @mentions

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    @peregrine I do not want to turn it off completely.

Sign In or Register to comment.