diff --git a/assets/javascripts/app/app.coffee b/assets/javascripts/app/app.coffee index 979ea35362..c337ac481b 100644 --- a/assets/javascripts/app/app.coffee +++ b/assets/javascripts/app/app.coffee @@ -25,6 +25,7 @@ @shortcuts = new app.Shortcuts @document = new app.views.Document @mobile = new app.views.Mobile if @isMobile() + @urls = new app.Urls if document.body.hasAttribute('data-doc') @DOC = JSON.parse(document.body.getAttribute('data-doc')) @@ -112,6 +113,7 @@ initDoc: (doc) -> doc.entries.add type.toEntry() for type in doc.types.all() @entries.add doc.entries.all() + @urls.addDoc doc return migrateDocs: -> diff --git a/assets/javascripts/app/urls.coffee b/assets/javascripts/app/urls.coffee new file mode 100644 index 0000000000..302e15d0ba --- /dev/null +++ b/assets/javascripts/app/urls.coffee @@ -0,0 +1,31 @@ +class app.Urls + _map: {} + _cached: {} + + get: (url) -> + return unless url + parsed = new URL(url) + @_cached?[parsed.host]?[_keyFor(parsed)] + + addDoc: (doc) -> + @_map[doc.slug] = doc.entries + .all() + .filter (entry) -> entry.url + .map (entry) -> [entry.url, entry.path] + @rebuild() + + removeDoc: (doc) -> + deleted = delete @_map[doc.slug] + @rebuild() if deleted + delted + + rebuild: -> + @_cached = {} + for slug, entries of @_map + for [url, path] in entries + parsed = new URL(url) + @_cached[parsed.host] ?= {} + @_cached[parsed.host][_keyFor(parsed)] = "/#{slug}/#{path}" + + +_keyFor = (parsed) -> parsed.pathname + parsed.search + parsed.hash diff --git a/assets/javascripts/models/entry.coffee b/assets/javascripts/models/entry.coffee index a8b2dd0160..ce9c6dce51 100644 --- a/assets/javascripts/models/entry.coffee +++ b/assets/javascripts/models/entry.coffee @@ -1,7 +1,7 @@ #= require app/searcher class app.models.Entry extends app.Model - # Attributes: name, type, path + # Attributes: name, type, path, url constructor: -> super diff --git a/assets/javascripts/views/content/entry_page.coffee b/assets/javascripts/views/content/entry_page.coffee index beae4d775a..e0b2a38e68 100644 --- a/assets/javascripts/views/content/entry_page.coffee +++ b/assets/javascripts/views/content/entry_page.coffee @@ -34,7 +34,9 @@ class app.views.EntryPage extends app.View $.batchUpdate @el, => @subview.render(content, fromCache) - @addCopyButtons() unless fromCache + unless fromCache + @addCopyButtons() + @internalizeLinks() return if app.disabledDocs.findBy 'slug', @entry.doc.slug @@ -55,6 +57,14 @@ class app.views.EntryPage extends app.View el.appendChild @copyButton.cloneNode(true) for el in @findAllByTag('pre') return + internalizeLinks: -> + for el in @findAllByTag('a') + continue if el.classList.contains '_attribution-link' + + internalUrl = app.urls.get(el.href) + if internalUrl? + el.href = internalUrl + polyfillMathML: -> return unless window.supportsMathML is false and !@polyfilledMathML and @findByTag('math') @polyfilledMathML = true diff --git a/lib/docs/core/models/entry.rb b/lib/docs/core/models/entry.rb index c3f5464c61..09a9c45cc5 100644 --- a/lib/docs/core/models/entry.rb +++ b/lib/docs/core/models/entry.rb @@ -4,22 +4,24 @@ module Docs class Entry class Invalid < StandardError; end - attr_accessor :name, :type, :path + attr_accessor :name, :type, :path, :url - def initialize(name = nil, path = nil, type = nil) + def initialize(name = nil, path = nil, type = nil, url = nil) self.name = name self.path = path self.type = type + self.url = url unless root? raise Invalid, 'missing name' if !name || name.empty? raise Invalid, 'missing path' if !path || path.empty? raise Invalid, 'missing type' if !type || type.empty? + raise Invalid, 'missing url' if !url || url.empty? end end def ==(other) - other.name == name && other.path == path && other.type == type + other.name == name && other.path == path && other.type == type && other.url == url end def name=(value) @@ -35,7 +37,7 @@ def root? end def as_json - { name: name, path: path, type: type } + { name: name, path: path, type: type, url: url } end end end diff --git a/lib/docs/filters/core/entries.rb b/lib/docs/filters/core/entries.rb index 43af4095a6..181c299819 100644 --- a/lib/docs/filters/core/entries.rb +++ b/lib/docs/filters/core/entries.rb @@ -56,8 +56,10 @@ def build_entries(entries) def build_entry(name, frag = nil, type = nil) type ||= self.type - path = frag ? (frag.include?('#') ? frag : "#{self.path}##{frag}") : self.path - Entry.new(name, path, type) + hash_frag = frag ? (frag.include?('#') ? frag : "##{frag}") : '' + path = "#{self.path}#{hash_frag}" + url = "#{current_url}#{hash_frag}" + Entry.new(name, path, type, url) end end end