Last active
May 9, 2016 15:53
-
-
Save avit/ed3afcf20c1e7feec1413f9ee468d9c0 to your computer and use it in GitHub Desktop.
Normalizer to add prefix on root-relative paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Normalizr.configure do |config| | |
# Match root-relative paths that begin with a leading slash | |
# | |
# (/path/to/image.jpg) in CSS url(/...) | |
# "/path/to/image.jpg" in HTML src="/..." | |
# '/path/to/image.jpg' in HTML src='/...' | |
# | |
# The contents of the parentheses or quotes are captured as matches that can | |
# be prefixed with a hostname for absolute URLs. | |
# | |
ROOT_RELATIVE_PATHS = %r{ | |
(?<= # Look behind before match start to find: | |
\( | # - an opening parenthesis, or | |
(['"]) # - an opening quote captured as \1 for closing pair | |
) # | |
(?= # Then, match the string that follows | |
/ # if it starts with a leading slash, | |
(?:\S+?) # has no whitespace and at least one character long, | |
(?: # and ends with either: | |
\1 # - the paired opening quote we captured | |
| \) # - or a closing parenthesis | |
) # | |
) # | |
}x | |
add :qualify_root_relative do |t, prefix:| | |
next unless t.present? && t.is_a?(String) | |
t.gsub(ROOT_RELATIVE_PATHS, prefix) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment