Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use ToC? #69

Closed
kim-tom opened this issue Mar 9, 2020 · 2 comments
Closed

How can I use ToC? #69

kim-tom opened this issue Mar 9, 2020 · 2 comments

Comments

@kim-tom
Copy link

kim-tom commented Mar 9, 2020

Thank you for your wonderful sophisticated package.
I can render markdowns this code.

processor = Qiita::Markdown::Processor.new(hostname: "example.com")
processor.call(markdown)[:output].to_s.html_safe

But how can I get a table of contents?? Can I give any options Processor.new()?
I should read your codes, but I' m sorry that I am unable to read your codes to understand how to use it.

@hareku
Copy link

hareku commented Mar 9, 2020

@kim-tom
We are sorry, but this library has not been maintained for a long time because Qiita uses a private repository renderer for some reasons.

However, I can give you advise. This repo does not provide TOC generator, but you can implement TOC filter like this.

module MyApp
  module Markdown
    module Filters
      class GreenmatWithTocFilter < HTML::Pipeline::TextFilter
        def call
          Nokogiri::HTML.fragment(greenmat.render(@text))
        end

        private

        def greenmat
          @renderer ||= ::Greenmat::Markdown.new(
            Qiita::Markdown::Greenmat::HTMLToCRenderer.new(escape_html: true),
            fenced_code_blocks: true,
            no_intra_emphasis: true,
            strikethrough: true,
          )
        end
      end
    end
  end
end

And implement a processor like this.

module MyApp
  module MarkdownProcessors
    class PublicTocProcessor < BaseMarkdownProcessor
      private

      def filters
        [
          ::MyApp::Markdown::Filters::PublicGreenmatWithTocFilter,
        ]
      end
    end
  end
end

@kim-tom
Copy link
Author

kim-tom commented Mar 9, 2020

Thank you for your very quick reply!
I'll try it!

@hareku hareku closed this as completed Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants