gitlab-org--gitlab-foss/lib/gitlab/dependency_linker/json_linker.rb

45 lines
1.2 KiB
Ruby
Raw Normal View History

2017-05-16 16:27:50 -04:00
module Gitlab
module DependencyLinker
class JsonLinker < BaseLinker
def link
return highlighted_text unless json
super
end
private
# Links package names in a JSON key or values.
#
# Example:
# link_json('name')
# # Will link `package` in `"name": "package"`
#
# link_json('name', 'specific_package')
# # Will link `specific_package` in `"name": "specific_package"`
#
# link_json('name', /[^\/]+\/[^\/]+/)
# # Will link `user/repo` in `"name": "user/repo"`, but not `"name": "package"`
#
# link_json('specific_package', '1.0.1', link: :key)
# # Will link `specific_package` in `"specific_package": "1.0.1"`
def link_json(key, value = nil, link: :value, &url_proc)
key = regexp_for_value(key, default: /[^" ]+/)
value = regexp_for_value(value, default: /[^" ]+/)
2017-05-16 16:27:50 -04:00
if link == :value
2017-05-25 09:55:47 -04:00
value = /(?<name>#{value})/
2017-05-16 16:27:50 -04:00
else
2017-05-25 09:55:47 -04:00
key = /(?<name>#{key})/
2017-05-16 16:27:50 -04:00
end
link_regex(/"#{key}":\s*"#{value}"/, &url_proc)
end
def json
@json ||= JSON.parse(plain_text) rescue nil
end
end
end
end