initialize markdown on a per-page basis

the footnote extra expects to only parse one document over the Markup's
lifetime, and writes the footnotes to the bottom of every page that is
rendered (again assuming only one) with links back to the reference

having one parser for the entire app, naturally, introduced
ever-increasing footnote links and every footnote on the site showing up
on every page. this was not intended

in some light testing, doing this per-request has a nominal effect on
performance
This commit is contained in:
Brian S. Stephan 2021-02-11 18:17:26 -06:00
parent b26ea6a661
commit 4f45943775
4 changed files with 28 additions and 15 deletions

View File

@ -3,7 +3,6 @@ import logging
import os
from logging.config import dictConfig
import markdown
from flask import Flask, request, send_from_directory
from ._version import get_versions
@ -32,10 +31,6 @@ def create_app(instance_path=None, test_config=None):
logger.debug("instance path: %s", app.instance_path)
# initialize markdown parser from config, but include
# extensions our app depends on, like the meta extension
app.config['md'] = markdown.Markdown(extensions=app.config['MARKDOWN_EXTENSIONS'] + ['meta'])
@app.before_request
def log_request():
logger.info("REQUEST: %s %s", request.method, request.path)

View File

@ -33,6 +33,7 @@ class Config(object):
}
MARKDOWN_EXTENSIONS = ['mdx_linkify', 'tables']
MARKDOWN_EXTENSION_CONFIGS = {}
DEFAULT_PAGE_STYLE = 'light'

View File

@ -1,7 +1,21 @@
"""Miscellaneous helper functions and whatnot."""
import markdown
from flask import current_app as app
def get_meta_str(key):
"""Provide the page's metadata for the specified key, or '' if unset."""
return " ".join(app.config['md'].Meta.get(key)) if app.config['md'].Meta.get(key) else ""
def get_meta_str(md, key):
"""Provide the page's (parsed in Markup obj md) metadata for the specified key, or '' if unset."""
return " ".join(md.Meta.get(key)) if md.Meta.get(key) else ""
def init_md():
"""Initialize the Markdown parser.
This used to done at the app level in __init__, but extensions like footnotes apparently
assume the parser to only live for the length of parsing one document, and create double
footnote ref links if the one parser sees the same document multiple times.
"""
# initialize markdown parser from config, but include
# extensions our app depends on, like the meta extension
return markdown.Markdown(extensions=app.config['MARKDOWN_EXTENSIONS'] + ['meta'],
extension_configs=app.config['MARKDOWN_EXTENSION_CONFIGS'])

View File

@ -8,7 +8,7 @@ from flask import current_app as app
from flask import make_response, redirect, render_template, request
from tzlocal import get_localzone
from incorporealcms.lib import get_meta_str
from incorporealcms.lib import get_meta_str, init_md
logger = logging.getLogger(__name__)
@ -34,11 +34,12 @@ def display_page(path):
logger.warning("requested path '%s' (resolved path '%s') not found!", path, resolved_path)
abort(404)
else:
content = Markup(app.config['md'].convert(entry))
logger.debug("file metadata: %s", app.config['md'].Meta)
md = init_md()
content = Markup(md.convert(entry))
logger.debug("file metadata: %s", md.Meta)
return render('base.html', title=get_meta_str('title'), description=get_meta_str('description'),
image=get_meta_str('image'), base_url=request.base_url, content=content, navs=parent_navs,
return render('base.html', title=get_meta_str(md, 'title'), description=get_meta_str(md, 'description'),
image=get_meta_str(md, 'image'), base_url=request.base_url, content=content, navs=parent_navs,
mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
@ -111,6 +112,8 @@ def generate_parent_navs(path):
else:
with app.open_instance_resource(resolved_path, 'r') as entry_file:
entry = entry_file.read()
_ = Markup(app.config['md'].convert(entry))
page_name = " ".join(app.config['md'].Meta.get('title')) if app.config['md'].Meta.get('title') else f'/{path}'
# for issues regarding parser reuse (see lib.init_md) we reinitialize the parser here
md = init_md()
_ = Markup(md.convert(entry))
page_name = " ".join(md.Meta.get('title')) if md.Meta.get('title') else f'/{path}'
return generate_parent_navs(parent_path) + [(page_name, f'/{path}')]