Skip to content
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

A new web component is needed to translate markdown into html sanitized #55

Open
UcDust opened this issue Apr 22, 2022 · 3 comments
Open
Assignees

Comments

@UcDust
Copy link
Collaborator

UcDust commented Apr 22, 2022

We use the marked npm package in the brands site to display class documentation. We need something similar in RP, to allow custom formatted markdown to be used to output people data. We could use another package like DOMPurify to sanitize the text.

<ucdlib-md ignore="H*"> - Means don't render it exclude="img" - Means excise it from the output subset?="core|full|parsimonious" - Specifies some common sets of ignores/excludes, that we use

The docs show the ability to override a list of renderer functions which would override the default renderer. We could use these to ignore specific tags if specified in the element attributes. From the docs-

const renderer = { heading(text, level) { return something; } };

Then marked.use({ renderer }); with our list of overridden render functions. In our case, we would return an empty string or perhaps the default text, depending on the element attributes.

We can override these functions:
Block-level renderer methods

code(string code, string infostring, boolean escaped)
blockquote(string quote)
html(string html)
heading(string text, number level, string raw, Slugger slugger)
hr()
list(string body, boolean ordered, number start)
listitem(string text, boolean task, boolean checked)
checkbox(boolean checked)
paragraph(string text)
table(string header, string body)
tablerow(string content)
tablecell(string content, object flags)

Inline-level renderer methods

strong(string text)
em(string text)
codespan(string code)
br()
del(string text)
link(string href, string title, string text)
image(string href, string title, string text)
text(string text)

These elements shadowDOM styling needs to match the patternLabs styling.

@UcDust UcDust changed the title ucdlib-theme is needed to translate markdown into html sanitized A new web component is needed to translate markdown into html sanitized Apr 22, 2022
@qjhart
Copy link
Contributor

qjhart commented Apr 22, 2022

@UcDust Looks pretty good to me. My thinking is that we can have a few default renderers that have our overridden functions for various types. Can you be thinking of how we can be showing examples of different MD renders?

@jrmerz
Copy link
Contributor

jrmerz commented Apr 22, 2022

IMHO, if you want flexible client side markdown rendering, just use the NPM packages. ie:

document.querySelector('#md-rendered').innerHTML = sanitize(mdRenderer(mdText));

I would suggest the advantages of a custom element, that we put time into supporting, would be that there is an opinion layer. So we are saying; this is our renderer, this is our sanitize utility, this is our standard setup. Of course you can allow for a couple optional params. The question I would have, is that even helpful? Perhaps a little. Our standard setup with single npm install command.

If we are going to implement a markdown element, the rendered markdown should be in the local dom. So if you have <ucdlib-md id="md"></ucdlib-md> and did something like document.querySelector("md").data = mdTxt then the markdown HTML would render into the local dom. Or if you wanted to declaratively set the markdown you could do something like:

<ucdlib-md id="md">
  <span class="md">raw markdown goes here</span>
</ucdlib-md>

And then the element hides the raw and inserts a new rendered tag as child element.

You could also look to do something like:

<ucdlib-md id="md">
  raw markdown goes here
</ucdlib-md>

Completely replacing the raw markdown with the rendered, however it doesn't provide a nice update cycle. ie it's rendered and done.

@qjhart
Copy link
Contributor

qjhart commented Apr 22, 2022

Doesn't the document.quewrySelector solution suffer from the same update problems? I guess one thing I think is nice is that the custom elements are more declarative, you add it in and it works.
Does it make sense to think about the old polymer "everything is an element" and have an engine-like thing:
<ucdlib-md-renderer render-class="md-rendered" subset="parsimonious"> which then sort of does what you suggested?

I guess markdown can get really messy anyways but is it better to use a tag and avoid things like <li class="md-rendered">

@UcDust UcDust self-assigned this May 2, 2022
jrmerz pushed a commit that referenced this issue May 3, 2022
* start of markdown component, still needs sanitize and more element classes to be used from the brands site #293

* add ol class and ability to customize renderer #55

* update brand documentation site with new ucdlib-md component

* add text sanitization to the md element #55

* update class documentation md

* brand site updates, increment package version, remove commented out code

Co-authored-by: Dusty Cartwright <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants