-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add styling for Markdown tables (#18)
* 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
Showing
9 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters