Skip to content

Commit

Permalink
Add styling for Markdown tables (#18)
Browse files Browse the repository at this point in the history
* Added defaults table styles to make them more readable

* Added final new line

* Resolved linting issues

* Added summary to the changelog

* Added table wrapping rule

- Updated scss with table wrapper
- Updated tests

* Added support for custom class name

* Updated README and JSDoc with the new plugin

* Got things inline with other plugins

* Fixed linting issues + typo
  • Loading branch information
savaryna authored Jul 11, 2022
1 parent d201297 commit 83a1ec5
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Each list item should be prefixed with `(patch)` or `(minor)` or `(major)`.
See `PUBLISH.md` for instructions on how to publish a new version.
-->

- (minor) Add styling for Markdown tables
- (minor) Add syntax for columns to customise layout
- (minor) Render captions for singleton images with titles

Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,39 @@ Set this property to `false` to disable this plugin.
_No options are available for this plugin._
</details>

### table_wrapper

<details>
<summary>Add wrapper element around Markdown `tables` for better controlled overflow.</summary>

No new syntax added. This just wraps normal Markdown `| a |` tables with a `div` that has a default
class of `table-wrapper`.

**Example Markdown input:**

| a |
|---|

**Example HTML output:**

<div class="table-wrapper">
<table>
<thead>
<tr>
<th>a</th>
</tr>
</thead>
</table>
</div>

**Options:**

Pass options for this plugin as the `table_wrapper` property of the `do-markdownit` plugin options.
Set this property to `false` to disable this plugin.

- `className` (`string`, optional, defaults to `'table-wrapper'`): Class to use for the table wrapper.
</details>

### callout

<details>
Expand Down Expand Up @@ -911,6 +944,7 @@ Variables listed here should be sorted based on the filename, and then by variab
| `$columns-inner-class` _(string)_ | `column` | The inner class name used for the `columns` plugin. | [`_columns.scss`](./styles/_columns.scss) |
| `$columns-outer-class` _(string)_ | `columns` | The outer class name used for the `columns` plugin. | [`_columns.scss`](./styles/_columns.scss) |
| `$rsvp-button-class` _(string)_ | `rsvp` | The class name used for the `rsvp_button` plugin. | [`_rsvp_button.scss`](./styles/_rsvp_button.scss) |
| `$table-wrapper-class` _(string)_ | `table-wrapper` | The class name used for the `table_wrapper` plugin. | [`_table_wrapper.scss`](./styles/_table_wrapper.scss) |
| `$terminal-button-class` _(string)_ | `terminal` | The class name used for the `terminal_button` plugin. | [`_terminal_button.scss`](./styles/_terminal_button.scss) |
| `$root-text-styles` _(boolean)_ | `true` | Enable or disable the `& {` selector for root text styles. | [`_typography.scss`](./styles/_typography.scss) |

Expand Down
4 changes: 2 additions & 2 deletions fixtures/full-output.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h2 id="step-1-basic-markdown">Step 1 — Basic Markdown</h2>
<li>On dangling single quotes: it’s</li>
<li>On en/em dashes: a – b, a — b</li>
</ul>
<table>
<div class="table-wrapper"><table>
<thead>
<tr>
<th>Tables</th>
Expand Down Expand Up @@ -117,7 +117,7 @@ <h2 id="step-1-basic-markdown">Step 1 — Basic Markdown</h2>
</tr>
</tbody>
</table>
<h2 id="step-2-code">Step 2 — Code</h2>
</div><h2 id="step-2-code">Step 2 — Code</h2>
<p>This is <code>inline code</code>. This is a <mark>variable</mark>. This is an <code>in-line code <mark>variable</mark></code>.</p>
<p>Here’s a configuration file with a label:</p>
<div class="code-label" title="/etc/nginx/sites-available/default">/etc/nginx/sites-available/default</div>
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const safeObject = require('./util/safe_object');
* @property {false|import('./rules/user_mention').UserMentionOptions} [user_mention] Disable user mentions, or set options for the feature.
* @property {false|import('./rules/html_comment').HtmlCommentOptions} [html_comment] Disable HTML comment stripping, or set options for the feature.
* @property {false} [image_caption] Disable image captions.
* @property {false|import('./rules/table_wrapper').TableWrapperOptions} [table_wrapper] Disable table wrapper, or set options for the feature.
* @property {false|import('./rules/embeds/callout').CalloutOptions} [callout] Disable callout block syntax, or set options for the feature.
* @property {false|import('./rules/embeds/rsvp_button').RsvpButtonOptions} [rsvp_button] Disable RSVP buttons, or set options for the feature.
* @property {false|import('./rules/embeds/terminal_button').TerminalButtonOptions} [terminal_button] Disable terminal buttons, or set options for the feature.
Expand Down Expand Up @@ -76,6 +77,10 @@ module.exports = (md, options) => {
md.use(require('./rules/image_caption'), safeObject(optsObj.image_caption));
}

if (optsObj.table_wrapper !== false) {
md.use(require('./rules/table_wrapper'), safeObject(optsObj.table_wrapper));
}

// Register embeds

if (optsObj.callout !== false) {
Expand Down
84 changes: 84 additions & 0 deletions rules/table_wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2022 DigitalOcean
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
http://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.
*/

'use strict';

/**
* @module rules/table_wrapper
*/

const safeObject = require('../util/safe_object');

/**
* @typedef {Object} TableWrapperOptions
* @property {string} [className='table-wrapper'] Class to use for the table wrapper.
*/

/**
* Add wrapper element around Markdown `tables` for better controlled overflow.
*
* No new syntax added. This just wraps normal Markdown `| a |` tables with a `div` that has a default
* class of `table-wrapper`.
*
* @example
* | a |
* |---|
*
* <div class="table-wrapper">
* <table>
* <thead>
* <tr>
* <th>a</th>
* </tr>
* </thead>
* </table>
* </div>
*
* @type {import('markdown-it').PluginWithOptions<TableWrapperOptions>}
*/
module.exports = (md, options) => {
const optsObj = safeObject(options);
const className = typeof optsObj.className === 'string' ? optsObj.className : 'table-wrapper';

/**
* Parsing rule for wrapping `tables` with a `div` with `table-wrapper` class.
*
* @type {import('markdown-it/lib/parser_core').RuleCore}
* @private
*/
const tableWrapperRule = state => {
state.tokens = state.tokens.reduce((newTokens, token) => {
// Add opening `div` with the `table-wrapper` class before all `table` opening tags
if (token.type === 'table_open') {
const tableWrapperOpen = new state.Token('div_open', 'div', 1);
tableWrapperOpen.attrSet('class', className);
newTokens.push(tableWrapperOpen);
newTokens.push(token);
// Add closing `div` after all `table` closing tags
} else if (token.type === 'table_close') {
const tableWrapperClose = new state.Token('div_close', 'div', -1);
newTokens.push(token);
newTokens.push(tableWrapperClose);
} else {
newTokens.push(token);
}

return newTokens;
}, []);
};

md.core.ruler.after('inline', 'table_wrapper', tableWrapperRule);
};
29 changes: 29 additions & 0 deletions rules/table_wrapper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2022 DigitalOcean
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
http://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.
*/

'use strict';

const md = require('markdown-it')().use(require('./table_wrapper'));

it('wraps a table with a `div` with the default `table-wrapper` class', () => {
expect(md.render('| a |\n|---|')).toBe('<div class="table-wrapper"><table>\n<thead>\n<tr>\n<th>a</th>\n</tr>\n</thead>\n</table>\n</div>');
});

const mdCustomClass = require('markdown-it')().use(require('./table_wrapper'), { className: 'custom-wrapper-class' });

it('wraps a table with a `div` with `custom-wrapper-class` class', () => {
expect(mdCustomClass.render('| a |\n|---|')).toBe('<div class="custom-wrapper-class"><table>\n<thead>\n<tr>\n<th>a</th>\n</tr>\n</thead>\n</table>\n</div>');
});
22 changes: 22 additions & 0 deletions styles/_table_wrapper.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2022 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License") !default;
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://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.
*/

$table-wrapper-class: "table-wrapper" !default;

.#{$table-wrapper-class} {
overflow-x: auto;
width: 100%;
}
56 changes: 56 additions & 0 deletions styles/_tables.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2022 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License") !default;
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://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 "theme";

table {
border-collapse: collapse;
width: 100%;

thead {
tr {
border-bottom: 1px dashed $gray7;

th {
color: $gray2;
font-weight: 600;
padding: 16px 8px;
}
}
}

tbody {
tr {
border-bottom: 1px solid $gray7;

td {
color: $gray3;
padding: 24px 8px;
}
}
}

th,
td {
&:first-child {
padding-left: 0;
}

&:last-child {
padding-right: 0;
}
}
}
2 changes: 2 additions & 0 deletions styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
@import "typography";
@import "images";
@import "code";
@import "tables";

// Plugin styling
@import "youtube";
Expand All @@ -30,3 +31,4 @@ limitations under the License.
@import "code_prefix";
@import "code_prism";
@import "code_secondary_label";
@import "table_wrapper";

0 comments on commit 83a1ec5

Please sign in to comment.