-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Figure out how to display images from <en-media> tags inline in Datasette #5
Comments
We could even do server-side thumbnailing for some of these images, but I'm inclined to serve up the full size ones and set a width on the image element based on the |
Alternatively, rather than relying on Maybe rename the column to Might need to feed them through Bleach too, just in case any nasty code can get into them. |
Or... I could do this client-side. JavaScript that looks for |
Maybe the best way do this is with a custom route, |
... but it's still important to be able to get to the rendered note directly from the browse notes |
Here's my first attempt at a plugin for this: from datasette import hookimpl
import jinja2
START = "<en-note"
END = "</en-note>"
TEMPLATE = """
<div style="max-width: 500px; white-space: normal; overflow-wrap: break-word;">{}</div>
""".strip()
EN_MEDIA_SCRIPT = """
Array.from(document.querySelectorAll('en-media')).forEach(el => {
let hash = el.getAttribute('hash');
let type = el.getAttribute('type');
let path = `/evernote/resources_data/${hash}.json?_shape=array`;
fetch(path).then(r => r.json()).then(rows => {
let b64 = rows[0].data.encoded;
let data = `data:${type};base64,${b64}`;
el.innerHTML = `<img style="max-width: 300px" src="${data}">`;
});
});
"""
@hookimpl
def render_cell(value, table):
if not table:
# Don't render content from arbitrary SQL queries, could be XSS hole
return
if not value or not isinstance(value, str):
return
value = value.strip()
if value.startswith(START) and value.endswith(END):
trimmed = value[len(START) : -len(END)]
trimmed = trimmed.split(">", 1)[1]
# Replace those horrible double newlines
trimmed = trimmed.replace("<div><br /></div>", "<br>")
return jinja2.Markup(TEMPLATE.format(trimmed))
@hookimpl
def extra_body_script():
return EN_MEDIA_SCRIPT It works! It does however demonstrate that Evernote's "clip this webpage" feature means there is a LOT of weird HTML that can get into a note. It looks like they've filtered out the scripts but I wouldn't bet on it - they certainly don't filter out many of the inline styles. So running Bleach is almost certainly a good idea. |
Relates to #1. Evernote XML looks like this:
That hash is the md5 we use to store resources. It should be possible to turn these into embedded image tags, especially if done in conjunction with the https://github.com/simonw/datasette-media plugin.
The text was updated successfully, but these errors were encountered: