Skip to content

Commit

Permalink
Add development setup using Webpack (#14)
Browse files Browse the repository at this point in the history
* Add webpack dev setup

* Configure eslint for dev setup

* Add to changelog
  • Loading branch information
MattIPv4 authored Jun 10, 2022
1 parent 12ef229 commit bc2b3a0
Show file tree
Hide file tree
Showing 11 changed files with 5,154 additions and 124 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ module.exports = {
jest: true,
},
},
{
files: 'dev/client.js',
env: {
browser: true,
},
},
],
rules: {
'space-before-function-paren': [
Expand Down Expand Up @@ -162,6 +168,15 @@ module.exports = {
array: false,
},
],
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: [
'**/*.test.js',
'dev/**/*.js',
],
},
],
'jsdoc/require-returns-description': 'off',
'jsdoc/tag-lines': 'off',
'jsdoc/no-undefined-types': [
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
vendor/prismjs/
jsdoc/
coverage/
dev/dist/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# Exclude all tests (though we do include the fixtures as demo content)
*.test.js

# Exclude all development files
dev/

# Exclude the vendored PrismJS files
vendor/prismjs/

Expand Down
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.
-->

- (patch) Add development setup using Webpack
- (minor) Add support for embedding CanIUse data
- (minor) Add support for embedding Glitch projects

Expand Down
68 changes: 68 additions & 0 deletions dev/client.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<!--
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.
-->
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0" />
<title>do-markdownit</title>
<style>
html,
body {
padding: 0;
margin: 0;
}

#app {
display: flex;
flex-direction: column;
}

#textbox,
#output {
padding: 1rem;
margin: 0;
}

#textbox {
border: none;
background: rgba(0, 0, 0, 0.05);
}

#output {
overflow: auto;
}

@media only screen and (min-width: 768px) {
#app {
display: flex;
flex-flow: row nowrap;
}

#textbox,
#output {
flex-basis: 50%;
}
}
</style>
</head>
<body>
<div id="app">
<textarea id="textbox"><%= text %></textarea>
<div id="output"><%= out %></div>
</div>
</body>
</html>
38 changes: 38 additions & 0 deletions dev/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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 render = require('./render');

document.addEventListener('DOMContentLoaded', () => {
const textbox = document.getElementById('textbox');
const output = document.getElementById('output');

/**
* Handle the textbox being updated.
* Resizes that textbox to match the input, renders the input to HTML.
*/
const update = () => {
textbox.style.overflowY = 'hidden';
textbox.style.height = 'auto';
textbox.style.height = `${textbox.scrollHeight}px`;
output.innerHTML = render(textbox.value);
};

textbox.addEventListener('input', update);
update();
});
45 changes: 45 additions & 0 deletions dev/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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')({
langPrefix: '',
linkify: true,
typographer: true,
}).use(require('..'), {
fence_environment: {
allowedEnvironments: [ 'local', 'second', 'third', 'fourth', 'fifth' ],
},
fence_classes: {
allowedClasses: [
'prefixed', 'line_numbers', 'command', 'super_user', 'custom_prefix',
...[ 'local', 'second', 'third', 'fourth', 'fifth' ].map(env => `environment-${env}`),
],
},
callout: {
allowedClasses: [ 'note', 'warning', 'info', 'draft' ],
},
});

/**
* Render a markdown string to HTML, using standard options for testing.
* Designed to be used with fixtures/full-input.md to generate/test fixtures/full-output.html
*
* @param {string} text Markdown string to render to HTML.
* @returns {string}
*/
module.exports = text => md.render(text);
42 changes: 42 additions & 0 deletions dev/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
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 HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const fs = require('fs');
const render = require('./render');

const text = fs.readFileSync(path.resolve(__dirname, '../fixtures/full-input.md'), 'utf8');

module.exports = {
mode: 'development',
entry: path.resolve(__dirname, './client.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'client.js',
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './client.html'),
templateParameters: {
text: text.replace(/</g, '&lt;').replace(/>/g, '&gt;'),
out: render(text),
},
}),
],
};
23 changes: 2 additions & 21 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,14 @@ limitations under the License.

const fs = require('fs');
const path = require('path');
const render = require('./dev/render');

const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'full-input.md'), 'utf8');
const output = fs.readFileSync(path.join(__dirname, 'fixtures', 'full-output.html'), 'utf8');

it('imports, loads and renders properly', () => {
// Create a new instance, testing the options the plugins support
const md = require('markdown-it')({
langPrefix: '',
linkify: true,
typographer: true,
}).use(require('.'), {
fence_environment: {
allowedEnvironments: [ 'local', 'second', 'third', 'fourth', 'fifth' ],
},
fence_classes: {
allowedClasses: [
'prefixed', 'line_numbers', 'command', 'super_user', 'custom_prefix',
...[ 'local', 'second', 'third', 'fourth', 'fifth' ].map(env => `environment-${env}`),
],
},
callout: {
allowedClasses: [ 'note', 'warning', 'info', 'draft' ],
},
});

// Render and check output
const rendered = md.render(input);
const rendered = render(input);
// fs.writeFileSync(path.join(__dirname, 'fixtures', 'full-output.html'), rendered);
expect(rendered).toBe(output);
});
Loading

0 comments on commit bc2b3a0

Please sign in to comment.