diff --git a/Gemfile b/Gemfile index bb500a6..3d42e01 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ +# frozen_string_literal: true source 'https://rubygems.org' gem 'commonmarker', '~> 0.16', require: false diff --git a/Rakefile b/Rakefile index f6ed6cf..13f3725 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'bundler/gem_tasks' require 'rake/testtask' diff --git a/graphql-docs.gemspec b/graphql-docs.gemspec index f63a840..d5bbabd 100644 --- a/graphql-docs.gemspec +++ b/graphql-docs.gemspec @@ -1,4 +1,5 @@ # coding: utf-8 +# frozen_string_literal: true lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'graphql-docs/version' diff --git a/lib/graphql-docs.rb b/lib/graphql-docs.rb index 52d4e7e..d1a326c 100644 --- a/lib/graphql-docs.rb +++ b/lib/graphql-docs.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'graphql-docs/helpers' require 'graphql-docs/renderer' require 'graphql-docs/configuration' diff --git a/lib/graphql-docs/configuration.rb b/lib/graphql-docs/configuration.rb index 435a6f4..4a6fe12 100644 --- a/lib/graphql-docs/configuration.rb +++ b/lib/graphql-docs/configuration.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module GraphQLDocs module Configuration GRAPHQLDOCS_DEFAULTS = { diff --git a/lib/graphql-docs/generator.rb b/lib/graphql-docs/generator.rb index e7af8f8..af8cd2c 100644 --- a/lib/graphql-docs/generator.rb +++ b/lib/graphql-docs/generator.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'erb' require 'sass' @@ -210,8 +211,9 @@ def write_file(type, name, contents, trim: true) contents.gsub!(/^\s{4}/m, ' ') end - contents = @renderer.render(contents, type: type, name: name) - File.write(File.join(path, 'index.html'), contents) unless contents.nil? + filename = File.join(path, 'index.html') + contents = @renderer.render(contents, type: type, name: name, filename: filename) + File.write(filename, contents) unless contents.nil? end end end diff --git a/lib/graphql-docs/helpers.rb b/lib/graphql-docs/helpers.rb index e9127e2..2c40a1f 100644 --- a/lib/graphql-docs/helpers.rb +++ b/lib/graphql-docs/helpers.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module GraphQLDocs module Helpers SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze diff --git a/lib/graphql-docs/parser.rb b/lib/graphql-docs/parser.rb index b114abc..9e22c74 100644 --- a/lib/graphql-docs/parser.rb +++ b/lib/graphql-docs/parser.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'graphql' module GraphQLDocs diff --git a/lib/graphql-docs/renderer.rb b/lib/graphql-docs/renderer.rb index 71a993c..0a1d6fd 100644 --- a/lib/graphql-docs/renderer.rb +++ b/lib/graphql-docs/renderer.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'html/pipeline' require 'yaml' require 'extended-markdown-filter' @@ -36,17 +37,17 @@ def initialize(parsed_schema, options) @pipeline = HTML::Pipeline.new(filters, @pipeline_config[:context]) end - def render(contents, type: nil, name: nil) - opts = { base_url: @options[:base_url] }.merge({ type: type, name: name}).merge(helper_methods) + def render(contents, type: nil, name: nil, filename: nil) + opts = { base_url: @options[:base_url], output_dir: @options[:output_dir] }.merge({ type: type, name: name, filename: filename}).merge(helper_methods) - contents = to_html(contents) + 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 - def to_html(string) - @pipeline.to_html(string) + def to_html(string, context: {}) + @pipeline.to_html(string, context) end private diff --git a/lib/graphql-docs/version.rb b/lib/graphql-docs/version.rb index db1bb19..cc4f947 100644 --- a/lib/graphql-docs/version.rb +++ b/lib/graphql-docs/version.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module GraphQLDocs VERSION = '1.4.1' end diff --git a/test/graphql-docs/generator_test.rb b/test/graphql-docs/generator_test.rb index 1d2ff54..62e8fe0 100644 --- a/test/graphql-docs/generator_test.rb +++ b/test/graphql-docs/generator_test.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'test_helper' class GeneratorTest < Minitest::Test @@ -5,7 +6,7 @@ class CustomRenderer def initialize(_, _) end - def render(contents, type: nil, name: nil) + def render(contents, type: nil, name: nil, filename: nil) to_html(contents) end diff --git a/test/graphql-docs/graphql-docs_test.rb b/test/graphql-docs/graphql-docs_test.rb index 327d383..28b6d3d 100644 --- a/test/graphql-docs/graphql-docs_test.rb +++ b/test/graphql-docs/graphql-docs_test.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'test_helper' class GraphQLDocsTest < Minitest::Test diff --git a/test/graphql-docs/parser_test.rb b/test/graphql-docs/parser_test.rb index a67b699..a54faa1 100644 --- a/test/graphql-docs/parser_test.rb +++ b/test/graphql-docs/parser_test.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'test_helper' class ParserTest < Minitest::Test diff --git a/test/graphql-docs/renderer_test.rb b/test/graphql-docs/renderer_test.rb index 284e0ee..35d6e7f 100644 --- a/test/graphql-docs/renderer_test.rb +++ b/test/graphql-docs/renderer_test.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'test_helper' class RendererTest < Minitest::Test @@ -19,4 +20,35 @@ def test_that_html_conversion_works assert_equal '

R2D2

', contents end + + def test_that_filename_accessible_to_filters + @renderer = GraphQLDocs::Renderer.new(@parsed_schema, GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS.merge({ + pipeline_config: { + pipeline: + %i(ExtendedMarkdownFilter + EmojiFilter + TableOfContentsFilter + AddFilenameFilter), + context: { + gfm: false, + asset_root: 'https://a248.e.akamai.net/assets.github.com/images/icons' + } + } + })) + contents = @renderer.render('', type: 'Droid', name: 'R2D2', filename: '/this/is/the/filename') + assert_match %r{/this/is/the/filename}, contents + end +end + +class AddFilenameFilter < HTML::Pipeline::Filter + def call + doc.search('span[@id="fill-me"]').each do |span| + span.inner_html=(context[:filename]) + end + doc + end + + def validate + needs :filename + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index be6d471..ed75626 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'graphql-docs'