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
|
# Build graph
|
||||||
graph = {}
|
graph = {}
|
||||||
|
backlink_graph = {}
|
||||||
for headline in all_headlines:
|
for headline in all_headlines:
|
||||||
links = []
|
links = []
|
||||||
headline_links = list(headline.get_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,
|
"target": headline.parent.id,
|
||||||
"relation": "in"
|
"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] = {
|
graph[headline.id] = {
|
||||||
"title": org_rw.org_rw.token_list_to_plaintext(headline.title.contents).strip(),
|
"title": org_rw.org_rw.token_list_to_plaintext(headline.title.contents).strip(),
|
||||||
"links": links,
|
"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 docs after we've built the graph
|
||||||
# Render main headlines
|
# 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():
|
for _docpath, main_headline in main_headlines_by_path.items():
|
||||||
if main_headline.doc.id:
|
if main_headline.doc.id:
|
||||||
endpath = os.path.join(dest_top, main_headline.doc.id + ".node.html")
|
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):
|
def render_results_block(element, acc, headline, graph):
|
||||||
items = [e.get_raw() for e in element.children]
|
items = [e.get_raw() for e in element.children]
|
||||||
print(items)
|
|
||||||
content = '\n'.join(items)
|
content = '\n'.join(items)
|
||||||
if len(content.strip()) > 0:
|
if len(content.strip()) > 0:
|
||||||
render_block(content, acc, _class='results lang-text', is_code=False)
|
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:
|
else:
|
||||||
return as_document(render(headline, doc, graph=graph, headlineLevel=headlineLevel), title)
|
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):
|
def render(headline, doc, graph, headlineLevel):
|
||||||
try:
|
try:
|
||||||
dom = headline.as_dom()
|
dom = headline.as_dom()
|
||||||
@ -586,6 +610,9 @@ def render(headline, doc, graph, headlineLevel):
|
|||||||
|
|
||||||
content = []
|
content = []
|
||||||
render_tree(dom, content, headline, graph)
|
render_tree(dom, content, headline, graph)
|
||||||
|
if headline.id:
|
||||||
|
render_connections(headline.id, content, graph)
|
||||||
|
|
||||||
for child in headline.children:
|
for child in headline.children:
|
||||||
content.append(render(child, doc, headlineLevel=headlineLevel+1, graph=graph))
|
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;
|
display: inline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.connections ul {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Headers */
|
/* Headers */
|
||||||
body > .node > h1 {
|
body > .node > h1 {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
Loading…
Reference in New Issue
Block a user