Skip to content

Commit

Permalink
Merge pull request #52 from ONSdigital/feature-update-markdown-proces…
Browse files Browse the repository at this point in the history
…sor-to-allow-captions

Feature update markdown processor to allow captions
  • Loading branch information
precious-onyenaucheya-ons authored Nov 3, 2023
2 parents 97e171c + 739476f commit c5b764e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/processors/markdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { marked } from "marked";
import { gfmHeadingId } from "marked-gfm-heading-id";

import markedExtension from "./marked-extension.js";
import StaticProcessor from "./processor.js";

const defaultOptions = {
Expand All @@ -15,6 +15,7 @@ const defaultOptions = {
};

marked.use(gfmHeadingId(false));
marked.use(markedExtension());
export default class MarkdownStaticProcessor extends StaticProcessor {
constructor(options) {
super();
Expand Down
28 changes: 28 additions & 0 deletions src/processors/marked-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This code defines a custom renderer that extends marked.js and overrides the table rendering method to include a <caption>.
// You can learn more about custom renderer here at https://marked.js.org/using_pro#renderer
export default function markedExtension() {
// Declare the heading text so it can be used globally
let headingText;
return {
renderer: {
// Custom renderer for headings
heading(text) {
// Set the heading text
headingText = text;
// Return an empty string to skip rendering the heading itself
return '';
},
// Custom renderer for tables
table(header, body){
// Check if there is heading text so we can add a caption
const caption = headingText ? `<caption>${headingText}</caption>` : '';
// Construct the table element with the optional caption
return `<table>
${caption}
<thead>${header}</thead>
<tbody>${body}</tbody>
</table>`;
}
}
};
}
3 changes: 1 addition & 2 deletions src/rendering/filters/htmlContentFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export default function createHtmlContentFilter(data) {
// Decorate table elements with ONS design system classes.
[escape("<table>")]: '<div class="ons-table-scrollable__content"><table class="ons-table ons-table--scrollable">',
[escape("</table>")]: '</table></div>',
[escape("<h2>")]: '<h3 class="ons-u-fs-m">',
[escape("</h2>")]: '</h3>',
[escape("<caption>")]: '<caption class="ons-table__caption ons-u-fs-m ons-u-mb-s ons-u-mt-l">',
[escape("<thead>")]: '<thead class="ons-table__head">',
[escape("<tbody>")]: '<tbody class="ons-table__body">',
[escape("<tr>")]: '<tr class="ons-table__row">',
Expand Down

0 comments on commit c5b764e

Please sign in to comment.