Упрощение генерации графа заметок: улучшена обработка узлов и рёбер, удалены неиспользуемые классы и методы. Обновлён формат сохранения данных в assets/js/graph-data.json.

This commit is contained in:
ada
2025-08-22 16:46:07 +03:00
parent 41fad17b9c
commit d1a18d191e
+19 -30
View File
@@ -1,50 +1,39 @@
# _plugins/note_graph.rb # _plugins/note_graph.rb
require "json" require "json"
require "fileutils"
module Jekyll module NoteGraph
class NoteGraphGenerator < Generator class Generator < Jekyll::Generator
safe true safe true
priority :low priority :low
def generate(site) def generate(site)
notes = site.collections["notes"].docs notes = (site.collections["notes"]&.docs) || []
nodes = [] nodes = []
links = [] links = []
notes.each do |note| notes.each do |doc|
# Добавляем узел title = (doc.data["title"] || doc.basename_without_ext).to_s
nodes << { id: note.data["title"] || note.basename_without_ext } nodes << { id: title, url: doc.url }
# Парсим wiki-ссылки [[...]] # [[Note]] / [[Note|alias]] / [[Note#Heading]] / [[Note#Heading|alias]]
content = note.content doc.content.to_s.scan(/\[\[([^\]|#]+)(?:#[^\]]*)?(?:\|[^\]]+)?\]\]/).flatten.each do |raw|
content.scan(/\[\[([^\]|]+)(?:\|[^\]]+)?\]\]/).flatten.each do |target| target = raw.strip
links << { links << { source: title, target: target }
source: note.data["title"] || note.basename_without_ext,
target: target
}
end end
end end
graph_hash = { nodes: nodes, links: links } graph = { nodes: nodes.uniq, links: links }
json_str = JSON.pretty_generate(graph_hash) site.config["note_graph_json"] = JSON.pretty_generate(graph)
end
site.static_files << GeneratedJson.new(site, json_str)
end end
end end
class GeneratedJson < Jekyll::StaticFile # Пишем файл ПОСЛЕ сборки сайта
def initialize(site, content) Jekyll::Hooks.register_once :site, :post_write do |site|
@site = site data = site.config["note_graph_json"] || "{}"
@content = content out_path = File.join(site.dest, "assets", "js", "graph-data.json")
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)) FileUtils.mkdir_p(File.dirname(out_path))
File.write(out_path, @content) File.write(out_path, data)
true
end
end
end end