중개인 및 루비에 익숙하지 않지만 클라이언트 측이 아닌 빌드 시간 동안 헤더의 측면 탐색/목록을 생성하도록 Slate을 작동 시키려고했습니다. 자바 스크립트를 사용하여. 내가 겪고있는 문제는 partial에서 헤더를 포함하는 코드를 얻는 것이다.Nokogiri를 사용하여 슬레이트/중개자에서 정적 헤더 목록을 생성합니다.
디렉토리 구조의 예 : 이것에 대한
Source
+--config.rb
+--includes
+--file.md
+--otherfile.md
+--index.html
+--layouts
+--layout.erb
Config.rb 조각이에 대한
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
레이아웃 미리보기 :
<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>
현재 index.html 파일의 헤더 만 채워지고 포함 된 부분은 없습니다. 나는 기존 도우미가 ready
도우미를 사용하여 similar to what is described in the Middleman docs for sitemaps 게시 빌드를 필요로 할 수도 있다고 생각합니다. 나는 그것이 추가 콘텐츠를 page_content
외부에서 캡처하도록 설정 코드를 또 변경해야한다고 생각하지만 친숙하지 않기 때문에 무엇인지 확신 할 수 없습니다. 모든 포인터는 감사하겠습니다.
편집 : 중개자 기본 문서를 살펴본 후 Padrino 프레임 워크에서 두 명의 도우미 인 : capture_html
및 concat_content
이있는 것으로 보입니다. 나는 도우미가 어디에서 특정 변경 사항에 대한 추가 컨텍스트를 얻기 위해 정의 된 page_content
을 찾으려고 노력하고있어.
나는 내가 중얼 거려야 할 중개자를 정의하기 위해 주위를 둘러 봐야 할 것처럼 보입니다. 시도해 볼만한 것을 주셔서 감사합니다! 이걸 가지고 계속 놀아 보겠다. # NameError : # <# 클래스 : 0x007fdd621f2650>에 대해 정의되지 않은 지역 변수 또는 메소드 full_content : \t layouts/apitwocolumn.erb : 92 :'singleton 클래스의 블록 ' \t 레이아웃/apitwocolumn.erb : -7 :'instance_eval에서' \t 레이아웃/apitwocolumn.erb : -7 : -9 :'싱글 톤 클래스의 \t 레이아웃/apitwocolumn.erb에서 __tilt_70294461913540'''''에서 – Alyss
이것은 실제로 실제로 최종 솔루션이었습니다! 코드를 조금 더 연구했으면 좋겠다. – Alyss