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

Support markdown tables in MediaExcerpts #716

Open
carlgieringer opened this issue Aug 25, 2024 · 2 comments
Open

Support markdown tables in MediaExcerpts #716

carlgieringer opened this issue Aug 25, 2024 · 2 comments
Labels
enhancement New feature or request ui Appearance and layout issues (not ux)

Comments

@carlgieringer
Copy link
Contributor

E.g.: https://www.howdju.com/media-excerpts/2021

@carlgieringer carlgieringer added enhancement New feature or request ui Appearance and layout issues (not ux) labels Aug 25, 2024
@carlgieringer
Copy link
Contributor Author

An approach adding just tables might look like:

import { Plugin } from "unified";
import { Node } from "unist";
import { visit } from "unist-util-visit";
import { gfmTable, gfmTableHtml } from "micromark-extension-gfm-table";
import { gfmTableFromMarkdown, gfmTableToMarkdown } from "mdast-util-gfm-table";

const remarkGfmTables: Plugin<[]> = function () {
  const data = this.data();

  add("micromarkExtensions", gfmTable);
  add("fromMarkdownExtensions", gfmTableFromMarkdown);
  add("toMarkdownExtensions", gfmTableToMarkdown);
  add("toHtmlExtensions", gfmTableHtml);

  function add(field: string, value: unknown) {
    const list = (data[field] || (data[field] = [])) as unknown[];
    list.push(value);
  }

  return (tree: Node) => {
    visit(tree, "text", (node: { type: string; value: string }) => {
      if (node.value.includes("|") && node.value.includes("\n")) {
        // This is a simple check for potential table content
        const lines = node.value.split("\n");
        if (lines.length >= 2 && lines[1].replace(/[^|]/g, "").length >= 3) {
          // This looks like it could be a table, so we'll mark it for processing
          node.type = "html";
        }
      }
    });
  };
};

export default remarkGfmTables;

@carlgieringer
Copy link
Contributor Author

#719 adds ~0.14 MB to the bundle size. I'm not sure how much that would reduce if we used the approach above for rendering just tables vs. all of remark-gfm.

Before:

Assets:
  main.css (4.77 MiB)
  premiser-ui.js (1.55 MiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
  main (6.32 MiB)
      main.css
      premiser-ui.js

After:

Assets:
  main.css (4.77 MiB)
  premiser-ui.js (1.69 MiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
  main (6.46 MiB)
      main.css
      premiser-ui.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ui Appearance and layout issues (not ux)
Projects
None yet
Development

No branches or pull requests

1 participant