From d1a18d191e6ea660b0277767f3582f9557b538de Mon Sep 17 00:00:00 2001 From: ada Date: Fri, 22 Aug 2025 16:46:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BF=D1=80=D0=BE=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B3=D1=80=D0=B0=D1=84=D0=B0=20=D0=B7=D0=B0=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=BA:=20=D1=83=D0=BB=D1=83=D1=87=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=83=D0=B7=D0=BB=D0=BE=D0=B2=20=D0=B8=20=D1=80?= =?UTF-8?q?=D1=91=D0=B1=D0=B5=D1=80,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BD=D0=B5=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=B7=D1=83=D0=B5=D0=BC=D1=8B=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D1=8B=20=D0=B8=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B.?= =?UTF-8?q?=20=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D1=91=D0=BD=20=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0=D1=82=20=D1=81=D0=BE=D1=85=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=B2=20assets/js/graph-data.json.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _plugins/note_graph.rb | 53 +++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/_plugins/note_graph.rb b/_plugins/note_graph.rb index 34e8f49..08ecbcf 100644 --- a/_plugins/note_graph.rb +++ b/_plugins/note_graph.rb @@ -1,50 +1,39 @@ # _plugins/note_graph.rb require "json" +require "fileutils" -module Jekyll - class NoteGraphGenerator < Generator +module NoteGraph + class Generator < Jekyll::Generator safe true priority :low def generate(site) - notes = site.collections["notes"].docs + notes = (site.collections["notes"]&.docs) || [] nodes = [] links = [] - notes.each do |note| - # Добавляем узел - nodes << { id: note.data["title"] || note.basename_without_ext } + notes.each do |doc| + title = (doc.data["title"] || doc.basename_without_ext).to_s + nodes << { id: title, url: doc.url } - # Парсим wiki-ссылки [[...]] - content = note.content - content.scan(/\[\[([^\]|]+)(?:\|[^\]]+)?\]\]/).flatten.each do |target| - links << { - source: note.data["title"] || note.basename_without_ext, - target: target - } + # [[Note]] / [[Note|alias]] / [[Note#Heading]] / [[Note#Heading|alias]] + doc.content.to_s.scan(/\[\[([^\]|#]+)(?:#[^\]]*)?(?:\|[^\]]+)?\]\]/).flatten.each do |raw| + target = raw.strip + links << { source: title, target: target } end end - graph_hash = { nodes: nodes, links: links } - json_str = JSON.pretty_generate(graph_hash) - - site.static_files << GeneratedJson.new(site, json_str) - end - end - - class GeneratedJson < Jekyll::StaticFile - def initialize(site, content) - @site = site - @content = content - super(site, site.dest, "assets/js", "graph-data.json", nil) - end - - def write(_dest) - out_path = @site.in_dest_dir("assets/js/graph-data.json") - FileUtils.mkdir_p(File.dirname(out_path)) - File.write(out_path, @content) - true + graph = { nodes: nodes.uniq, links: links } + site.config["note_graph_json"] = JSON.pretty_generate(graph) end end end + +# Пишем файл ПОСЛЕ сборки сайта +Jekyll::Hooks.register_once :site, :post_write do |site| + data = site.config["note_graph_json"] || "{}" + out_path = File.join(site.dest, "assets", "js", "graph-data.json") + FileUtils.mkdir_p(File.dirname(out_path)) + File.write(out_path, data) +end