mirror of
https://github.com/ada-dmitry/ada-dmitry.github.io.git
synced 2026-09-24 09:20:40 +00:00
Упрощение генерации графа заметок: улучшена обработка узлов и рёбер, удалены неиспользуемые классы и методы. Обновлён формат сохранения данных в assets/js/graph-data.json.
This commit is contained in:
+21
-32
@@ -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)
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user