-
Notifications
You must be signed in to change notification settings - Fork 838
[RFC] Split markdown rendering to a separate package. #595
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
base: main
Are you sure you want to change the base?
Changes from all commits
0053287
56f8236
352ed82
a6d1f4d
b108738
05df1da
7074758
8b9dc13
6d5cd33
3215107
5b01ced
ae76503
bd8da1f
62f1de7
d0555e7
e07cef8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||
| /* | ||||||
| Copyright 2025 Google LLC | ||||||
|
|
||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| you may not use this file except in compliance with the License. | ||||||
| You may obtain a copy of the License at | ||||||
|
|
||||||
| https://www.apache.org/licenses/LICENSE-2.0 | ||||||
|
|
||||||
| Unless required by applicable law or agreed to in writing, software | ||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| See the License for the specific language governing permissions and | ||||||
| limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| import {markdownContext} from "./markdown.js"; | ||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? I need to setup some form of "format on save" or something I guess? |
||||||
| import {themeContext} from "./theme.js"; | ||||||
|
|
||||||
| export { | ||||||
| markdownContext as markdown, | ||||||
| themeContext as theme, | ||||||
| themeContext, // Preserved for backwards compatibility. Prefer using `theme`. | ||||||
| }; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| Copyright 2025 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| import { | ||
| DirectiveResult, | ||
| } from "lit/directive.js"; | ||
| import { createContext } from "@lit/context"; | ||
|
|
||
| /** | ||
| * A Lit Context to override the default (noop) markdown renderer. | ||
| */ | ||
| export const markdownContext = createContext<DirectiveResult>( | ||
| Symbol("a2ui-lit-markdown-renderer") | ||
| ); |
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is now probably redundant. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| Copyright 2025 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| import { html, TemplateResult } from "lit"; | ||
| import { | ||
| directive, | ||
| Directive, | ||
| PartInfo, | ||
| } from "lit/directive.js"; | ||
|
|
||
| /** | ||
| * A minimal Markdown renderer that only supports a few tags. | ||
| * | ||
| * Configure @a2ui/lit-markdown, or your custom Markdown renderer | ||
| * to actually parse and render Markdown in your app. | ||
| */ | ||
| class MinimalMarkdownRendererDirective extends Directive { | ||
| static _warned = false; | ||
|
|
||
| constructor(partInfo: PartInfo) { | ||
| super(partInfo); | ||
| /** | ||
| * Warn the user in case they need actual Markdown rendering. | ||
| */ | ||
| if (!MinimalMarkdownRendererDirective._warned) { | ||
| console.warn("[MinimalMarkdownRendererDirective]", | ||
| "is a placeholder Markdown renderer. Most features are not supported.\n", | ||
| "Install `@a2ui/lit-markdown` for a fully-featured Markdown renderer."); | ||
| MinimalMarkdownRendererDirective._warned = true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to render some Markdown. | ||
| * | ||
| * Resist the urge to "improve" this method. Instead, use a more proper | ||
| * Markdown renderer. This is just a placeholder. | ||
| */ | ||
| render(markdown: string, _tagClassMap?: Record<string, string[]>) : TemplateResult { | ||
| if (markdown.startsWith('# ')) { | ||
| let classes = this.getClasses("h1", _tagClassMap); | ||
| return html`<h1 class="${classes}">${markdown.substring(2)}</h1>`; | ||
| } | ||
| if (markdown.startsWith('## ')) { | ||
| let classes = this.getClasses("h2", _tagClassMap); | ||
| return html`<h2 class="${classes}">${markdown.substring(3)}</h2>`; | ||
| } | ||
| if (markdown.startsWith('### ')) { | ||
| let classes = this.getClasses("h3", _tagClassMap); | ||
| return html`<h3 class="${classes}">${markdown.substring(4)}</h3>`; | ||
| } | ||
| if (markdown.startsWith('#### ')) { | ||
| let classes = this.getClasses("h4", _tagClassMap); | ||
| return html`<h4 class="${classes}">${markdown.substring(5)}</h4>`; | ||
| } | ||
| if (markdown.startsWith('##### ')) { | ||
| let classes = this.getClasses("h5", _tagClassMap); | ||
| return html`<h5 class="${classes}">${markdown.substring(6)}</h5>`; | ||
| } | ||
| if (markdown.startsWith('*') && markdown.endsWith('*')) { | ||
| let classes = this.getClasses("strong", _tagClassMap); | ||
| return html`<strong class="${classes}">${markdown.substring(1, markdown.length - 1)}</strong>`; | ||
| } | ||
| let classes = this.getClasses("p", _tagClassMap); | ||
| return html`<p class="${classes}">${markdown}</p>`; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a space-separated lists of clases for `tag` from `_tagClassMap`. | ||
| * @param tag The name of the tag ("h1", "p", etc.) | ||
| * @param _tagClassMap The class map to use. | ||
| * @returns A space-separated list of class names. | ||
| */ | ||
| getClasses(tag: string, _tagClassMap?: Record<string, string[]>) { | ||
| return _tagClassMap?.[tag]?.join(' ') ?? ''; | ||
| } | ||
| } | ||
|
|
||
| export const minimalMarkdown = directive(MinimalMarkdownRendererDirective); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| This directory contains the default markdown implementations for A2UI. | ||
|
|
||
| * `markdown-it-shared` is a shared markdown renderer that uses markdown-it and | ||
| DOMPurify to render markdown content in general. | ||
| * `markdown-it-lit` is the Lit facade of the markdown renderer for Lit. | ||
| * `markdown-it-angular` is the Angular facade of the markdown renderer | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Haha, aspirational :P |
||
| for Angular. | ||
|
|
||
| Users should use the facade packages in their apps. There's nothing of interest | ||
| in the shared renderer package. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Is this needed?) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| @a2ui:registry=https://us-npm.pkg.dev/oss-exit-gate-prod/a2ui--npm/ | ||
| //us-npm.pkg.dev/oss-exit-gate-prod/a2ui--npm/:always-auth=true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| A2UI markdown renderer for Lit. |
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.
Can we stop comitting the lockfiles to the repo? They add a lot of noise and can cause unnecessary merge trouble! Any advantage of checking this in??