Module: LinkifyRe

Included in:
Linkify
Defined in:
lib/linkify-it-rb/re.rb

Constant Summary collapse

SRC_ANY =

Use direct extract instead of ‘regenerate` to reduce size

UCMicro::Properties::Any::REGEX.source
SRC_CC =
UCMicro::Categories::Cc::REGEX.source
SRC_Z =
UCMicro::Categories::Z::REGEX.source
SRC_P =
UCMicro::Categories::P::REGEX.source
SRC_Z_P_CC =

pZPCcCf (white spaces + control + format + punctuation)

[ SRC_Z, SRC_P, SRC_CC ].join('|')
SRC_Z_CC =

pZCc (white spaces + control)

[ SRC_Z, SRC_CC ].join('|')
TEXT_SEPARATORS =

Experimental. List of chars, completely prohibited in links because can separate it from other part of text

'[><\uff5c]'
SRC_PSEUDO_LETTER =

All possible word characters (everything without punctuation, spaces & controls) Defined via punctuation & spaces to save space Should be something like pLNSM (w but without ‘_`)

'(?:(?!' + TEXT_SEPARATORS + '|' + SRC_Z_P_CC + ')' + SRC_ANY + ')'
SRC_IP4 =

'(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
SRC_AUTH =

Prohibit any of “@/[]()” in user/pass to avoid wrong domain fetch.

'(?:(?:(?!' + SRC_Z_CC + '|[@/\\[\\]()]).)+@)?'
SRC_PORT =
'(?::(?:6(?:[0-4]\\d{3}|5(?:[0-4]\\d{2}|5(?:[0-2]\\d|3[0-5])))|[1-5]?\\d{1,4}))?'
SRC_HOST_TERMINATOR =
'(?=$|' + TEXT_SEPARATORS + '|' + SRC_Z_P_CC + ')(?!-|_|:\\d|\\.-|\\.(?!$|' + SRC_Z_P_CC + '))'
SRC_EMAIL_NAME =

moved SRC_PATH into re_src_path

'[\\-;:&=\\+\\$,\\"\\.a-zA-Z0-9_]+'
SRC_XN =
'xn--[a-z0-9\\-]{1,59}'
SRC_DOMAIN_ROOT =

More to read about domain names serverfault.com/questions/638260/

'(?:' +
  SRC_XN +
  '|' +
  SRC_PSEUDO_LETTER + '{1,63}' +
')'
SRC_DOMAIN =
'(?:' +
  SRC_XN +
  '|' +
  '(?:' + SRC_PSEUDO_LETTER + ')' +
  '|' +
  # don't allow `--` in domain names, because:
  # - that can conflict with markdown &mdash; / &ndash;
  # - nobody use those anyway
  '(?:' + SRC_PSEUDO_LETTER + '(?:-(?!-)|' + SRC_PSEUDO_LETTER + '){0,61}' + SRC_PSEUDO_LETTER + ')' +
')'
SRC_HOST =
'(?:' +
# Don't need IP check, because digits are already allowed in normal domain names
# SRC_IP4 +
# '|' +
  '(?:(?:(?:' + SRC_DOMAIN + ')\\.)*' + SRC_DOMAIN + ')' +
')'
TPL_HOST_FUZZY =
'(?:' +
  SRC_IP4 +
'|' +
  '(?:(?:(?:' + SRC_DOMAIN + ')\\.)+(?:%TLDS%))' +
')'
TPL_HOST_NO_IP_FUZZY =
'(?:(?:(?:' + SRC_DOMAIN + ')\\.)+(?:%TLDS%))'
SRC_HOST_STRICT =
SRC_HOST + SRC_HOST_TERMINATOR
TPL_HOST_FUZZY_STRICT =
TPL_HOST_FUZZY + SRC_HOST_TERMINATOR
SRC_HOST_PORT_STRICT =
SRC_HOST + SRC_PORT + SRC_HOST_TERMINATOR
TPL_HOST_PORT_FUZZY_STRICT =
TPL_HOST_FUZZY + SRC_PORT + SRC_HOST_TERMINATOR
TPL_HOST_PORT_NO_IP_FUZZY_STRICT =
TPL_HOST_NO_IP_FUZZY + SRC_PORT + SRC_HOST_TERMINATOR
TPL_HOST_FUZZY_TEST =

Rude test fuzzy links by host, for quick deny

'localhost|www\\.|\\.\\d{1,3}\\.|(?:\\.(?:%TLDS%)(?:' + SRC_Z_P_CC + '|>|$))'
TPL_EMAIL_FUZZY =
'(^|' + TEXT_SEPARATORS + '|\\(|' +SRC_Z_CC + ')(' + SRC_EMAIL_NAME + '@' + TPL_HOST_FUZZY_STRICT + ')'

Instance Method Summary collapse

Instance Method Details

#build_re(opts) ⇒ Object




99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/linkify-it-rb/re.rb', line 99

def build_re(opts)
  re = {
    src_Any:              SRC_ANY,
    src_Cc:               SRC_CC,
    src_Z:                SRC_Z,
    src_P:                SRC_P,
    src_XPCc:             SRC_Z_P_CC,
    src_ZCc:              SRC_Z_CC,
    src_pseudo_letter:    SRC_PSEUDO_LETTER,
    src_ip4:              SRC_IP4,
    src_auth:             SRC_AUTH,
    src_port:             SRC_PORT,
    src_host_terminator:  SRC_HOST_TERMINATOR,
    src_path:             re_src_path(opts),
    src_email_name:       SRC_EMAIL_NAME,
    src_xn:               SRC_XN,
    src_domain_root:      SRC_DOMAIN_ROOT,
    src_domain:           SRC_DOMAIN,
    src_host:             SRC_HOST,

    tpl_host_fuzzy:                   TPL_HOST_FUZZY,
    tpl_host_no_ip_fuzzy:             TPL_HOST_NO_IP_FUZZY,
    src_host_strict:                  SRC_HOST_STRICT,
    tpl_host_fuzzy_strict:            TPL_HOST_FUZZY_STRICT,
    src_host_port_strict:             SRC_HOST_PORT_STRICT,
    tpl_host_port_fuzzy_strict:       TPL_HOST_PORT_FUZZY_STRICT,
    tpl_host_port_no_ip_fuzzy_strict: TPL_HOST_PORT_NO_IP_FUZZY_STRICT,

    tpl_host_fuzzy_test:  TPL_HOST_FUZZY_TEST,
    tpl_email_fuzzy:      TPL_EMAIL_FUZZY
  }

  # Fuzzy link can't be prepended with .:/\- and non punctuation.
  # but can start with > (markdown blockquote)
  re[:tpl_link_fuzzy] =
      '(^|(?![.:/\\-_@])(?:[$+<=>^`|\uff5c]|' + SRC_Z_P_CC + '))' +
      '((?![$+<=>^`|\uff5c])' + TPL_HOST_PORT_FUZZY_STRICT + re[:src_path] + ')'

  # Fuzzy link can't be prepended with .:/\- and non punctuation.
  # but can start with > (markdown blockquote)
  re[:tpl_link_no_ip_fuzzy] =
    '(^|(?![.:/\\-_@])(?:[$+<=>^`|\uff5c]|' + SRC_Z_P_CC + '))' +
    '((?![$+<=>^`|\uff5c])' + TPL_HOST_PORT_NO_IP_FUZZY_STRICT + re[:src_path] + ')'

  return re
end

#re_src_path(opts = nil) ⇒ Object




147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/linkify-it-rb/re.rb', line 147

def re_src_path(opts = nil)
    '(?:' +
      '[/?#]' +
        '(?:' +
          '(?!' + SRC_Z_CC + '|' + TEXT_SEPARATORS + '|[()\\[\\]{}.,"\'?!\\-]).|' +
          '\\[(?:(?!' + SRC_Z_CC + '|\\]).)*\\]|' +
          '\\((?:(?!' + SRC_Z_CC + '|[)]).)*\\)|' +
          '\\{(?:(?!' + SRC_Z_CC + '|[}]).)*\\}|' +
          '\\"(?:(?!' + SRC_Z_CC + '|["]).)+\\"|' +
          "\\'(?:(?!" + SRC_Z_CC + "|[']).)+\\'|" +
          "\\'(?=" + SRC_PSEUDO_LETTER + '|[-]).|' +  # allow `I'm_king` if no pair found
          '\\.{2,3}[a-zA-Z0-9%/]|' + # github has ... in commit range links. Restrict to
                                     # - english
                                     # - percent-encoded
                                     # - parts of file path
                                     # until more examples found.
          '\\.(?!' + SRC_Z_CC + '|[.]).|' +
          (opts && opts[:'---'] ?
            '\\-(?!--(?:[^-]|$))(?:-*)|'  # `---` => long dash, terminate
          :
            '\\-+|'
          ) +
          '\\,(?!' + SRC_Z_CC + ').|' +      # allow `,,,` in paths
          '\\!(?!' + SRC_Z_CC + '|[!]).|' +
          '\\?(?!' + SRC_Z_CC + '|[?]).' +
        ')+' +
      '|\\/' +
    ')?'
end