Module: Ariadne::Yard::LookbookDocsHelper

Defined in:
lib/ariadne/yard/lookbook_docs_helper.rb

Overview

Helper methods for documentation generated in Lookbook pages.

Instance Method Summary collapse

Instance Method Details

Adheres to the same signature as Ariadne::Yard::DocsHelper#link_to_component so link_to_component may be used in a Gatsby or Lookbook context and produce the correct link for each platform.

Parameters:

  • component (Class)

    The component class to link to.

Returns:

  • (String)

    The link, either in HTML or markdown format.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ariadne/yard/lookbook_docs_helper.rb', line 13

def link_to_component(component)
  backend = Ariadne::Yard::LookbookPagesBackend.new(Ariadne::Yard::Registry.make, nil)
  component_ref = Ariadne::Yard::ComponentManifest.ref_for(component)
  page = backend.page_for(component_ref)

  # If the page_path method is available, we're being rendered into HTML by Lookbook
  # and should emit an HTML <a> tag. No page_path means we're being rendered into
  # markdown by LookbookPagesBackend and should emit a markdown + ERB link that
  # Lookbook will eventually render on page load.
  if respond_to?(:page_path)
    link_to(page.docs.short_name, page_path(page.page_id.to_sym.inspect))
  else
    # rubocop:disable Rails/OutputSafety
    "[#{page.docs.short_name}](<%= page_path(#{page.page_id.to_sym.inspect}) %>)".html_safe
    # rubocop:enable Rails/OutputSafety
  end
end