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_EMAIL_NAME =

Allow anything in markdown spec, forbid quote (“) at the first position because emails enclosed in quotes are far more common

'[\\-;:&=\\+\\$,\\.a-zA-Z0-9_][\\-;:&=\\+\\$,\\"\\.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 + ')' +
  '|' +
  '(?:' + 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%))'
TPL_HOST_FUZZY_TEST =

Rude test fuzzy links by host, for quick deny

'localhost|www\\.|\\.\\d{1,3}\\.|(?:\\.(?:%TLDS%)(?:' + SRC_Z_P_CC + '|>|$))'

Instance Method Summary collapse

Instance Method Details

#build_re(opts = {}) ⇒ Object




91
92
93
94
95
96
97
98
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
# File 'lib/linkify-it-rb/re.rb', line 91

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:  re_src_host_terminator(opts),
    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 + re_src_host_terminator(opts),
    tpl_host_fuzzy_strict:            TPL_HOST_FUZZY + re_src_host_terminator(opts),
    src_host_port_strict:             SRC_HOST + SRC_PORT + re_src_host_terminator(opts),
    tpl_host_port_fuzzy_strict:       TPL_HOST_FUZZY + SRC_PORT + re_src_host_terminator(opts),
    tpl_host_port_no_ip_fuzzy_strict: TPL_HOST_NO_IP_FUZZY + SRC_PORT + re_src_host_terminator(opts),

    tpl_host_fuzzy_test:  TPL_HOST_FUZZY_TEST
  }

  re[:tpl_email_fuzzy] =
    '(^|' + TEXT_SEPARATORS + '|"|\\(|' + SRC_Z_CC + ')' +
    '(' + SRC_EMAIL_NAME + '@' + re[:tpl_host_fuzzy_strict] + ')'

  # 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])' + re[: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])' + re[:tpl_host_port_no_ip_fuzzy_strict] + re[:src_path] + ')'

  return re
end

#re_src_host_terminator(opts = {}) ⇒ Object




177
178
179
180
# File 'lib/linkify-it-rb/re.rb', line 177

def re_src_host_terminator(opts = {})
  '(?=$|' + TEXT_SEPARATORS + '|' + SRC_Z_P_CC + ')' +
  '(?!' + (opts['---'] ? '-(?!--)|' : '-|') + '_|:\\d|\\.-|\\.(?!$|' + SRC_Z_P_CC + '))'
end

#re_src_path(opts = {}) ⇒ Object




142
143
144
145
146
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
# File 'lib/linkify-it-rb/re.rb', line 142

def re_src_path(opts = {})
  '(?:' +
    '[/?#]' +
      '(?:' +
        '(?!' + 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,}[a-zA-Z0-9%/&]|' + # google has many dots in "google search" links (#66, #81).
                                   # github has ... in commit range links,
                                   # Restrict to
                                   # - english
                                   # - percent-encoded
                                   # - parts of file path
                                   # - params separator
                                   # until more examples found.
        '\\.(?!' + SRC_Z_CC + '|[.]|$)|' +
        (opts && opts[:'---'] ?
          '\\-(?!--(?:[^-]|$))(?:-*)|'  # `---` => long dash, terminate
        :
          '\\-+|'
        ) +
        '\\,(?!' + SRC_Z_CC + '|$)|' +      # allow `,,,` in paths
        '\\;(?!' + SRC_Z_CC + '|$)|' +      # allow `;` if not followed by space-like char
        '\\!+(?!' + SRC_Z_CC + '|[!]|$)|' + # allow `!!!` in paths, but not at the end
        '\\?(?!' + SRC_Z_CC + '|[?]|$)' +
      ')+' +
    '|\\/' +
  ')?'
end