Add links in markdown export for browser viewer

This commit is contained in:
Kovid Goyal 2021-01-06 14:57:06 +05:30
parent fa85e012dd
commit 13d829c74a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -5,7 +5,7 @@ from __python__ import bound_methods, hash_literals
from elementmaker import E from elementmaker import E
from ajax import encode_query from ajax import encode_query
from book_list.globals import get_session_data from book_list.globals import get_session_data, get_current_query
from book_list.theme import get_color from book_list.theme import get_color
from complete import create_search_bar from complete import create_search_bar
from dom import add_extra_css, build_rule, clear, svgicon, unique_id from dom import add_extra_css, build_rule, clear, svgicon, unique_id
@ -525,13 +525,14 @@ def get_container():
return document.getElementById(get_container_id()) return document.getElementById(get_container_id())
def render_highlight_as_text(hl, lines, as_markdown=False, link_prefix=None): def render_highlight_as_text(hl, lines, link_prefix, current_query, as_markdown=False):
lines.push(hl.highlighted_text) lines.push(hl.highlighted_text)
date = Date(hl.timestamp).toLocaleString() date = Date(hl.timestamp).toLocaleString()
if as_markdown and link_prefix: if as_markdown:
cfi = hl.start_cfi cfi = hl.start_cfi
spine_index = (1 + hl.spine_index) * 2 spine_index = (1 + hl.spine_index) * 2
link = link_prefix + encode_query({'open_at': f'epubcfi(/{spine_index}{cfi})'}) current_query.bookpos = f'epubcfi(/{spine_index}{cfi})'
link = link_prefix + encode_query(current_query)[1:].replace(/\)/g, '%29')
date = f'[{date}]({link})' date = f'[{date}]({link})'
lines.push(date) lines.push(date)
notes = hl.notes notes = hl.notes
@ -554,6 +555,12 @@ def show_export_dialog(annotations_manager):
fmt = 'text' fmt = 'text'
all_highlights = annotations_manager.all_highlights() all_highlights = annotations_manager.all_highlights()
ta_id = unique_id() ta_id = unique_id()
href = window.location.href
idx = href.indexOf('#')
href = href[:idx+1]
current_query = Object.assign({}, get_current_query())
link_prefix = window.location.href
link_prefix = link_prefix[:link_prefix.indexOf('#') + 1]
def update_text(): def update_text():
if fmt is 'calibre_annotations_collection': if fmt is 'calibre_annotations_collection':
@ -567,7 +574,7 @@ def show_export_dialog(annotations_manager):
as_markdown = fmt is 'markdown' as_markdown = fmt is 'markdown'
lines = v'[]' lines = v'[]'
for hl in all_highlights: for hl in all_highlights:
render_highlight_as_text(hl, lines, as_markdown=as_markdown) render_highlight_as_text(hl, lines, link_prefix, current_query, as_markdown=as_markdown)
document.getElementById(ta_id).textContent = lines.join('\n') document.getElementById(ta_id).textContent = lines.join('\n')
def fmt_item(text, val): def fmt_item(text, val):
@ -625,7 +632,6 @@ def find(backwards):
all_highlights.reverse() all_highlights.reverse()
q = text.toLowerCase() q = text.toLowerCase()
for h in all_highlights: for h in all_highlights:
console.log(h.dataset.title)
if h.dataset.title.toLowerCase().indexOf(q) > -1 or h.dataset.notes.toLowerCase().indexOf(q) > -1: if h.dataset.title.toLowerCase().indexOf(q) > -1 or h.dataset.notes.toLowerCase().indexOf(q) > -1:
set_current_highlight_entry(h) set_current_highlight_entry(h)
h.scrollIntoView() h.scrollIntoView()