Class: GraphQLDocs::Renderer

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/graphql-docs/renderer.rb

Overview

Renders documentation content into HTML.

The Renderer takes parsed schema content and converts it to HTML using html-pipeline. It applies markdown processing, emoji support, and other filters, then wraps the result in the default layout template.

Examples:

Basic usage

renderer = GraphQLDocs::Renderer.new(parsed_schema, options)
html = renderer.render(markdown_content, type: 'object', name: 'User')

Custom renderer

class MyRenderer < GraphQLDocs::Renderer
  def render(contents, type: nil, name: nil, filename: nil)
    # Custom rendering logic
    super
  end
end

Constant Summary

Constants included from Helpers

Helpers::SLUGIFY_PRETTY_REGEXP

Instance Attribute Summary collapse

Attributes included from Helpers

#templates

Instance Method Summary collapse

Methods included from Helpers

#graphql_directive_types, #graphql_enum_types, #graphql_input_object_types, #graphql_interface_types, #graphql_mutation_types, #graphql_object_types, #graphql_operation_types, #graphql_query_types, #graphql_root_types, #graphql_scalar_types, #graphql_union_types, #include, #markdownify, #slugify, #split_into_metadata_and_contents, #yaml?, #yaml_split

Constructor Details

#initialize(parsed_schema, options) ⇒ Renderer

Initializes a new Renderer instance.

Parameters:

  • parsed_schema (Hash)

    The parsed schema from Parser#parse

  • options (Hash)

    Configuration options

Options Hash (options):

  • :templates (Hash)

    Template file paths

  • :pipeline_config (Hash)

    html-pipeline configuration



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/graphql-docs/renderer.rb', line 39

def initialize(parsed_schema, options)
  @parsed_schema = parsed_schema
  @options = options

  @graphql_default_layout = ERB.new(File.read(@options[:templates][:default])) unless @options[:templates][:default].nil?

  @pipeline_config = @options[:pipeline_config] || {}
  pipeline = @pipeline_config[:pipeline] || {}
  context = @pipeline_config[:context] || {}

  filters = pipeline.map do |f|
    if filter?(f)
      f
    else
      key = filter_key(f)
      filter = HTML::Pipeline.constants.find { |c| c.downcase == key }
      # possibly a custom filter
      if filter.nil?
        Kernel.const_get(f)
      else
        HTML::Pipeline.const_get(filter)
      end
    end
  end

  @pipeline = HTML::Pipeline.new(filters, context)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/graphql-docs/renderer.rb', line 31

def options
  @options
end

Instance Method Details

#render(contents, type: nil, name: nil, filename: nil) ⇒ String?

Renders content into complete HTML with layout.

This method converts the content through the html-pipeline filters and wraps it in the default layout template. If the method returns nil, no file will be written.

Examples:

html = renderer.render(markdown, type: 'object', name: 'User')

Parameters:

  • contents (String)

    Content to render (typically Markdown)

  • type (String, nil) (defaults to: nil)

    GraphQL type category (e.g., 'object', 'interface')

  • name (String, nil) (defaults to: nil)

    Name of the GraphQL type being rendered

  • filename (String, nil) (defaults to: nil)

    Output filename path

Returns:

  • (String, nil)

    Rendered HTML content, or nil to skip file generation



80
81
82
83
84
85
86
87
88
89
# File 'lib/graphql-docs/renderer.rb', line 80

def render(contents, type: nil, name: nil, filename: nil)
  # Include all options (like Generator does) to support YAML frontmatter variables like title
  opts = @options.merge({type: type, name: name, filename: filename}).merge(helper_methods)

  contents = to_html(contents, context: {filename: filename})
  return contents if @graphql_default_layout.nil?

  opts[:content] = contents
  @graphql_default_layout.result(OpenStruct.new(opts).instance_eval { binding })
end

#to_html(string, context: {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a string to HTML using html-pipeline.

Parameters:

  • string (String)

    Content to convert

  • context (Hash) (defaults to: {})

    Additional context for pipeline filters

Returns:

  • (String)

    HTML output from pipeline



98
99
100
# File 'lib/graphql-docs/renderer.rb', line 98

def to_html(string, context: {})
  @pipeline.to_html(string, context)
end