Module: GraphQLDocs::Helpers
Overview
Helper methods module for use in ERB templates.
This module provides utility methods that can be called from within ERB templates
when generating documentation. Methods are available via dot notation in templates,
such as <%= slugify.(text) %>
.
Constant Summary collapse
- SLUGIFY_PRETTY_REGEXP =
Regular expression for slugifying strings in a URL-friendly way. Matches all characters that are not alphanumeric or common URL-safe characters.
Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze
Instance Attribute Summary collapse
-
#templates ⇒ Hash
Cache of loaded ERB templates.
Instance Method Summary collapse
-
#graphql_directive_types ⇒ Array<Hash>
Returns all directive types from the parsed schema.
-
#graphql_enum_types ⇒ Array<Hash>
Returns all enum types from the parsed schema.
-
#graphql_input_object_types ⇒ Array<Hash>
Returns all input object types from the parsed schema.
-
#graphql_interface_types ⇒ Array<Hash>
Returns all interface types from the parsed schema.
-
#graphql_mutation_types ⇒ Array<Hash>
Returns all mutation types from the parsed schema.
-
#graphql_object_types ⇒ Array<Hash>
Returns all object types from the parsed schema.
-
#graphql_operation_types ⇒ Array<Hash>
Returns all operation types (Query, Mutation) from the parsed schema.
-
#graphql_query_types ⇒ Array<Hash>
Returns all query types from the parsed schema.
-
#graphql_root_types ⇒ Hash
Returns the root types (query, mutation) from the parsed schema.
-
#graphql_scalar_types ⇒ Array<Hash>
Returns all scalar types from the parsed schema.
-
#graphql_union_types ⇒ Array<Hash>
Returns all union types from the parsed schema.
-
#include(filename, opts = {}) ⇒ String
Includes and renders a partial template file.
-
#markdownify(string) ⇒ String
Converts a Markdown string to HTML.
-
#slugify(str) ⇒ String
Converts a string into a URL-friendly slug.
-
#split_into_metadata_and_contents(contents, parse: true) ⇒ Array<(Hash, String), (String, String)>
Splits content into YAML front matter metadata and body content.
-
#yaml?(contents) ⇒ Boolean
Checks if content starts with YAML front matter.
-
#yaml_split(contents) ⇒ Array<String>
Splits content by YAML front matter delimiters.
Instance Attribute Details
#templates ⇒ Hash
Returns Cache of loaded ERB templates.
23 24 25 |
# File 'lib/graphql-docs/helpers.rb', line 23 def templates @templates end |
Instance Method Details
#graphql_directive_types ⇒ Array<Hash>
Returns all directive types from the parsed schema.
144 145 146 |
# File 'lib/graphql-docs/helpers.rb', line 144 def graphql_directive_types @parsed_schema[:directive_types] || [] end |
#graphql_enum_types ⇒ Array<Hash>
Returns all enum types from the parsed schema.
116 117 118 |
# File 'lib/graphql-docs/helpers.rb', line 116 def graphql_enum_types @parsed_schema[:enum_types] || [] end |
#graphql_input_object_types ⇒ Array<Hash>
Returns all input object types from the parsed schema.
130 131 132 |
# File 'lib/graphql-docs/helpers.rb', line 130 def graphql_input_object_types @parsed_schema[:input_object_types] || [] end |
#graphql_interface_types ⇒ Array<Hash>
Returns all interface types from the parsed schema.
109 110 111 |
# File 'lib/graphql-docs/helpers.rb', line 109 def graphql_interface_types @parsed_schema[:interface_types] || [] end |
#graphql_mutation_types ⇒ Array<Hash>
Returns all mutation types from the parsed schema.
95 96 97 |
# File 'lib/graphql-docs/helpers.rb', line 95 def graphql_mutation_types @parsed_schema[:mutation_types] || [] end |
#graphql_object_types ⇒ Array<Hash>
Returns all object types from the parsed schema.
102 103 104 |
# File 'lib/graphql-docs/helpers.rb', line 102 def graphql_object_types @parsed_schema[:object_types] || [] end |
#graphql_operation_types ⇒ Array<Hash>
Returns all operation types (Query, Mutation) from the parsed schema.
81 82 83 |
# File 'lib/graphql-docs/helpers.rb', line 81 def graphql_operation_types @parsed_schema[:operation_types] || [] end |
#graphql_query_types ⇒ Array<Hash>
Returns all query types from the parsed schema.
88 89 90 |
# File 'lib/graphql-docs/helpers.rb', line 88 def graphql_query_types @parsed_schema[:query_types] || [] end |
#graphql_root_types ⇒ Hash
Returns the root types (query, mutation) from the parsed schema.
74 75 76 |
# File 'lib/graphql-docs/helpers.rb', line 74 def graphql_root_types @parsed_schema[:root_types] || [] end |
#graphql_scalar_types ⇒ Array<Hash>
Returns all scalar types from the parsed schema.
137 138 139 |
# File 'lib/graphql-docs/helpers.rb', line 137 def graphql_scalar_types @parsed_schema[:scalar_types] || [] end |
#graphql_union_types ⇒ Array<Hash>
Returns all union types from the parsed schema.
123 124 125 |
# File 'lib/graphql-docs/helpers.rb', line 123 def graphql_union_types @parsed_schema[:union_types] || [] end |
#include(filename, opts = {}) ⇒ String
Includes and renders a partial template file.
This method loads an ERB template from the includes directory and renders it with the provided options. Useful for reusing template fragments.
50 51 52 53 54 |
# File 'lib/graphql-docs/helpers.rb', line 50 def include(filename, opts = {}) template = fetch_include(filename) opts = { base_url: @options[:base_url], classes: @options[:classes] }.merge(opts) template.result(OpenStruct.new(opts.merge(helper_methods)).instance_eval { binding }) end |
#markdownify(string) ⇒ String
Converts a Markdown string to HTML.
64 65 66 67 68 69 |
# File 'lib/graphql-docs/helpers.rb', line 64 def markdownify(string) return '' if string.nil? type = @options[:pipeline_config][:context][:unsafe] ? :UNSAFE : :DEFAULT ::CommonMarker.render_html(string, type).strip end |
#slugify(str) ⇒ String
Converts a string into a URL-friendly slug.
33 34 35 36 37 |
# File 'lib/graphql-docs/helpers.rb', line 33 def slugify(str) slug = str.gsub(SLUGIFY_PRETTY_REGEXP, '-') slug.gsub!(/^\-|\-$/i, '') slug.downcase end |
#split_into_metadata_and_contents(contents, parse: true) ⇒ Array<(Hash, String), (String, String)>
Splits content into YAML front matter metadata and body content.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/graphql-docs/helpers.rb', line 156 def (contents, parse: true) pieces = yaml_split(contents) raise "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format." if pieces.size < 4 # Parse begin = if parse YAML.safe_load(pieces[2]) || {} else pieces[2] end rescue Exception => e # rubocop:disable Lint/RescueException raise "Could not parse YAML for #{name}: #{e.}" end [, pieces[4]] end |
#yaml?(contents) ⇒ Boolean
Checks if content starts with YAML front matter.
177 178 179 |
# File 'lib/graphql-docs/helpers.rb', line 177 def yaml?(contents) contents =~ /\A-{3,5}\s*$/ end |
#yaml_split(contents) ⇒ Array<String>
Splits content by YAML front matter delimiters.
185 186 187 |
# File 'lib/graphql-docs/helpers.rb', line 185 def yaml_split(contents) contents.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3) end |