Add simple backlink connections.
This commit is contained in:
parent
08d35fc0b5
commit
4849128fcd
@ -182,6 +182,7 @@ def regen_all(src_top, dest_top, *, docs=None, db=None):
|
||||
|
||||
# Build graph
|
||||
graph = {}
|
||||
backlink_graph = {}
|
||||
for headline in all_headlines:
|
||||
links = []
|
||||
headline_links = list(headline.get_links())
|
||||
@ -216,6 +217,19 @@ def regen_all(src_top, dest_top, *, docs=None, db=None):
|
||||
"target": headline.parent.id,
|
||||
"relation": "in"
|
||||
})
|
||||
for backlink in links:
|
||||
if 'relation' in backlink and backlink['relation'] == 'in':
|
||||
continue
|
||||
|
||||
target = backlink['target']
|
||||
if target.startswith('id:'):
|
||||
target = target[len('id:'):]
|
||||
|
||||
if target not in backlink_graph:
|
||||
backlink_graph[target] = set()
|
||||
|
||||
backlink_graph[target].add(headline.id)
|
||||
|
||||
graph[headline.id] = {
|
||||
"title": org_rw.org_rw.token_list_to_plaintext(headline.title.contents).strip(),
|
||||
"links": links,
|
||||
@ -247,7 +261,7 @@ def regen_all(src_top, dest_top, *, docs=None, db=None):
|
||||
|
||||
# Render docs after we've built the graph
|
||||
# Render main headlines
|
||||
full_graph_info = { "nodes": graph, "main_headlines": main_headlines_by_path }
|
||||
full_graph_info = { "nodes": graph, "backlinks": backlink_graph, "main_headlines": main_headlines_by_path }
|
||||
for _docpath, main_headline in main_headlines_by_path.items():
|
||||
if main_headline.doc.id:
|
||||
endpath = os.path.join(dest_top, main_headline.doc.id + ".node.html")
|
||||
@ -428,7 +442,6 @@ def render_code_block(element, acc, headline, graph):
|
||||
|
||||
def render_results_block(element, acc, headline, graph):
|
||||
items = [e.get_raw() for e in element.children]
|
||||
print(items)
|
||||
content = '\n'.join(items)
|
||||
if len(content.strip()) > 0:
|
||||
render_block(content, acc, _class='results lang-text', is_code=False)
|
||||
@ -576,6 +589,17 @@ def render_as_document(headline, doc, headlineLevel, graph, title):
|
||||
else:
|
||||
return as_document(render(headline, doc, graph=graph, headlineLevel=headlineLevel), title)
|
||||
|
||||
def render_connections(headline_id, content, graph):
|
||||
if headline_id not in graph['backlinks']:
|
||||
return
|
||||
|
||||
content.append("<div class='connections'><span class='backlink-explanation'>These notes link here:</span><ul>")
|
||||
for backlink in sorted(graph['backlinks'][headline_id], key=lambda x: graph['nodes'][x]['title']):
|
||||
link = graph["nodes"][backlink]
|
||||
title = link["title"]
|
||||
content.append(f"<li><a class='internal backlink' href='./{backlink}.node.html'>{html.escape(title)}</a></li>")
|
||||
content.append("</ul></div>")
|
||||
|
||||
def render(headline, doc, graph, headlineLevel):
|
||||
try:
|
||||
dom = headline.as_dom()
|
||||
@ -586,6 +610,9 @@ def render(headline, doc, graph, headlineLevel):
|
||||
|
||||
content = []
|
||||
render_tree(dom, content, headline, graph)
|
||||
if headline.id:
|
||||
render_connections(headline.id, content, graph)
|
||||
|
||||
for child in headline.children:
|
||||
content.append(render(child, doc, headlineLevel=headlineLevel+1, graph=graph))
|
||||
|
||||
|
@ -173,6 +173,10 @@ h1 p,h2 p,h3 p,h4 p,h5 p,h6 p, li p {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
.connections ul {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
/* Headers */
|
||||
body > .node > h1 {
|
||||
text-align: center;
|
||||
|
Loading…
Reference in New Issue
Block a user