Using Nokogiri to create a static list of headers in Slate / Middleman

I am inexperienced with a middleman and ruby, but I tried to get Slate so that it creates side navigation / list of the header during build, and not on the client side using javascript. The problem I am facing is that the code includes partial headers.

Example directory structure:

Source +--config.rb +--includes +--file.md +--otherfile.md +--index.html +--layouts +--layout.erb 

The essence of the layout and config.rb

Config.rb file for this:

 require 'nokogiri' helpers do def toc_data(page_content) html_doc = Nokogiri::HTML::DocumentFragment.parse(page_content) # get a flat list of headers headers = [] html_doc.css('h1, h2, h3').each do |header| headers.push({ id: header.attribute('id').to_s, content: header.content, level: header.name[1].to_i, children: [] }) end [3,2].each do |header_level| header_to_nest = nil headers = headers.reject do |header| if header[:level] == header_level header_to_nest[:children].push header if header_to_nest true else header_to_nest = header if header[:level] == (header_level - 1) false end end end headers end end 

Layout for this:

 <ul id="toc" class="toc"> <% toc_data(page_content).each do |h1| %> <li> <a href="#<%= h1[:id] %>" class="toc-h1"><%= h1[:content] %></a> <ul class="toc-section"> <% h1[:children].each do |h2| %> <li> <a href="#<%= h2[:id] %>" class="toc-h2"><%= h2[:content] %></a> <ul class="toc-submenu"> <% h2[:children].each do |h3| %> <li> <a href="#<%= h3[:id] %>" class="toc-h3"><%= h3[:content] %></a> </li> <% end %> </ul> </li> <% end %> </ul> </li> <% end %> </ul> ... <div class="page-wrapper"> <div class="content"> <%= page_content %> <% current_page.data.includes && current_page.data.includes.each do |include| %> <%= partial "includes/#{include}" %> <% end %> </div> </div> 

Currently, only the headers from the index.html file and none of the parts included in it are filled. I believe that I might need an existing helper to create a post-build, similar to what Middleman docs for Sitemaps use with the ready helper. I believe that I need to make another change to the configuration code so that it captures additional content outside of page_content , but I'm not sure that this is due to a lack of acquaintance. Any pointers would be appreciated.

Edit: After looking at the broker's manual documents, there are two helpers from the Padrino structure that I could use: capture_html and concat_content . I am trying to find where the page_content helper page_content defined in order to get additional context for the specific changes I am making.

+5
source share
2 answers

To associate the current page data with particles using page_content, use the code below. It also changes everything you need to get a full page.

 <% if current_page.data.includes current_page.data.includes.each do |include| page_content += partial("includes/#{include}") end end %> ... <%= page_content %> 
0
source

Not familiar with this structure, but looks like toc_data(page_content) only for the main content, and not for partial current_page.data.includes .

So guess what you need to pass a partial part of your toc_data function.

Maybe it works?

 <% full_content = page_content current_page.data.includes && current_page.data.includes.each do |include| full_content += partial("includes/#{include}") end toc_data(full_content).each do |h1| %> ... <% end %> 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1261597/


All Articles