Skip to content

Commit

Permalink
Convert external links to other enabled docs to internal links
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 committed Nov 4, 2017
1 parent 8e2a918 commit 0f6cef2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions assets/javascripts/app/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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: ->
Expand Down
31 changes: 31 additions & 0 deletions assets/javascripts/app/urls.coffee
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion assets/javascripts/models/entry.coffee
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion assets/javascripts/views/content/entry_page.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions lib/docs/core/models/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
6 changes: 4 additions & 2 deletions lib/docs/filters/core/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0f6cef2

Please sign in to comment.