-
Notifications
You must be signed in to change notification settings - Fork 162
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
client-side narrative parsing #1172
Conversation
… file This is in preparation for client-side parsing of narrative files. A optional `type` URL query allows the same API to return the markdown file (see the documentation changes in this commit for more details). There are other possibilities, such as using the content-type field in the header, but I felt this was the clearest.
Here we add the framework to (eventually) parse the response from `getNarrative` as a markdown file and interpret this client-side. To facilitate backward compatibility in the cases where a server does not respect the `?type=md` query and continues to return a JSON, if the file doesn't look like markdown we will attempt to parse it as JSON. To mimic such a server, we explicitly request "type=json" in this commit, as the code to actually do the client-side md-parsing is not yet implemented.
ce70b81
to
b61af18
Compare
This commit shifts the parsing of the markdown file (md + yaml frontmatter) into a centralised function which is imported by both the server and the client, allowing us to maintain backward compatible server-side parsing as well as client-side parsing, whilst avoiding code duplication and the inevitable divergence of functionality. Our approach to conversion of YAML frontmatter -> markdown is essentially unchanged. The algorithm for parsing of the main body has been rewritten to be more robust. Unit tests are added to cover the various ways we can define authors, links & affiliations in the YAML frontmatter of narratives.
Parsing of the narrative (YAML) frontmatter produces markdown with `<sup>` and `<sub>` HTML elements to render affiliations using superscripts. (This behavior is unchanged from parsing server-side.) The client-side markdown sanitizer settings, however, were stricter than those employed on the server, and were removing these HTML elements causing the affiliations not to be displayed. These settings are relaxed in this commit.
b61af18
to
a0cf89e
Compare
The move to client-parsing of narratives has caused two changes to bundle structure. Firstly, since the `loadJSON` function now references the markdown parsing code, the dependency graph tries to pull in the related libraries into the chunk with most of the Auspice code (currently chunk 5). Previously these were a separate chunk lazily loaded with (e.g.) the footer parsing component. Since it's very common to display markdown footers, it makes life simple to include these libraries in the other-vendors bundle. It is possible to lazily load the markdown parsing function as needed, but more complicated. Secondly, the YAML frontmatter parser (previously server-side only) has a dependency on `js-yaml` which itself has dependency on "esprima" and others. Esprima is unnecessary for our purposes and we use a webpack loader to ignore it. For simplicity, we shift `js-yaml` to the other-vendors bundle. An alternative approach would be to bundle pre-minified 'js-yaml' bundle.
For unknown reasons, the core-vendors and other-vendors chunks have different hashes when building locally compared with via GitHub CI. This is after running `npm ci` to mimic the CI conditions. An issue should be created to better understand this, as it implies that the contents differ.
e039d39
to
de9439f
Compare
Auspice PR nextstrain/auspice#1172 allows narrative parsing to be performed client-side. Once that PR is merged, auspice will make `getNarrative` GET requests with a `type=md` query and expect the markdown content to be returned as the response (although it will still handle a JSON response for backwards compatability.) This PR updates the nextstrain.org server's `/charon/getNarrative` handler to stream the markdown file as a response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @jameshadfield! Parsing does indeed look more robust (and in practice, handles a recent issue with narrative parsing gracefully) and more importantly, we can feel more confident knowing that any parsing issues will manifest in client errors - making them transparent for the user and less impactful to nextstrain.org as you have pointed out.
As we discussed offline, after rebasing on master, unit tests pass and any failures of integration tests seem like they are not introduced by this PR.
const mainMarkdownLines = []; | ||
let inMainMarkdownSection = false; | ||
slide.lines.forEach((line) => { | ||
if (!inMainMarkdownSection && line.match(/^```auspiceMainDisplayMarkdown\s*$/)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to allow for chars (whitespace or otherwise) on the line before ```auspiceMainDisplayMarkdown and/or non-whitespace chars on that same line after that? I.e. is it important to strictly specify the opening and closing tags for auspiceMainDisplayMarkdown on their own lines without any other characters on those lines? If the answer is yes, that seems perfectly reasonable to me, just checking this is the intention while I'm scrutinizing auspiceMainDisplayMarkdown parsing after recently working on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we want to require the line to not have leading whitespace, following markdown syntax for fenced code blocks.
Tangentially, when I implemented this ability it was in a needs-must situation and I didn't foresee how popular it'd be. I can imagine us deprecating the fenced-code-block syntax for something else in the future, e.g. ## Main Screen Markdown
.
Auspice PR nextstrain/auspice#1172 allows narrative parsing to be performed client-side. Once that PR is merged, auspice will make `getNarrative` GET requests with a `type=md` query and expect the markdown content to be returned as the response (although it will still handle a JSON response for backwards compatability.) This PR updates the nextstrain.org server's `/charon/getNarrative` handler to stream the markdown file as a response.
Auspice PR nextstrain/auspice#1172 allows narrative parsing to be performed client-side. Once that PR is merged, auspice will make `getNarrative` GET requests with a `type=md` query and expect the markdown content to be returned as the response (although it will still handle a JSON response for backwards compatability.) This PR updates the nextstrain.org server's `/charon/getNarrative` handler to stream the markdown file as a response.
The narrative markdown file has historically been transformed into JSON format on the server. This is unnecessary and has multiple disadvantages. This PR changes the (default) behavior of auspice so that narrative markdown content is parsed client-side.
The following situations have been tested (here "old" means current master, "new" means this PR)
The client-side and server-side parsing of the narrative file is done using the same code with one exception: while they both use the
marked
library to convert markdown to HTML, they each use different (sanitizer) settings. The server-side parsing uses the unchanged settings from before, however on the client we use the existingparseMarkdown
function. For example, the author affiliations (deriving from YAML frontmatter and appearing on the opening slide of a narrative) use the following "markdown":However the content inside
<sub><sup>
tags is being removed when parsed on the client. There may be other situations (e.g. embedded tweets).Testing
Unit tests added to test various possible formats of the YAML frontmatter.
Advantages of client-side parsing
In addition the parsing of narrative files has been made more robust in this PR (this applies to both server-side and client-side parsing as we use the same code).
For more information please see the commit messages.
Outstanding tasks:
use-narratives-to-test
and run integration tests