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

Add practice exercise markdown #1486

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,20 @@
"strings"
]
},
{
"slug": "markdown",
"name": "Markdown",
"uuid": "3994ee58-0d75-4c46-afe1-666466470255",
"practices": ["regular-expressions", "functions"],
"prerequisites": [],
"difficulty": 4,
"topics": [
"conditionals",
"pattern_recognition",
"regular_expressions",
"strings"
]
},
{
"slug": "book-store",
"name": "Book Store",
Expand Down
15 changes: 15 additions & 0 deletions exercises/practice/markdown/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Description

Refactor a Markdown parser.

The markdown exercise is a refactoring exercise. There is code that parses a
given string with [Markdown
syntax](https://guides.github.com/features/mastering-markdown/) and returns the
associated HTML for that string. Even though this code is confusingly written
and hard to follow, somehow it works and all the tests are passing! Your
challenge is to re-write this code to make it easier to read and maintain
while still making sure that all the tests keep passing.

It would be helpful if you made notes of what you did in your refactoring in
comments so reviewers can see that, but it isn't strictly necessary. The most
important thing is to make the code better!
14 changes: 14 additions & 0 deletions exercises/practice/markdown/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
"excludedFiles": ["custom.spec.js"],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
10 changes: 10 additions & 0 deletions exercises/practice/markdown/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"blurb": "Refactor a Markdown parser.",
"authors": ["kmelow"],
"contributors": [],
"files": {
"solution": ["markdown.js"],
"test": ["markdown.spec.js"],
"example": [".meta/proof.ci.js"]
}
}
72 changes: 72 additions & 0 deletions exercises/practice/markdown/.meta/proof.ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// This is only a SKELETON file for the 'Matrix' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const parse = (markdown) => {
if (markdown.includes(' __')) {
markdown = markdown.replace(' __', ' <strong>');
}
if (markdown.includes(' _')) {
markdown = markdown.replace(' _', ' <em>');
}

if (markdown.includes('__ ')) {
markdown = markdown.replace('__ ', '</strong> ');
}
if (markdown.includes('_ ')) {
markdown = markdown.replace('_ ', '</em> ');
}

if (markdown.match(/__\w/g)) {
markdown = markdown.replace('__', '<strong>');
}
if (markdown.match(/\w__/g)) {
markdown = markdown.replace('__', '</strong>');
}

if (markdown.match(/_\w/g)) {
markdown = markdown.replace('_', '<em>');
}
if (markdown.match(/\w_/g)) {
markdown = markdown.replace('_', '</em>');
}

if (markdown.startsWith('# ')) {
markdown = markdown.replace('# ', '<h1>');
if (markdown.match(/\n\*/gm)) {
markdown = markdown.replace('\n* ', '</h1><ul><li>');
markdown = markdown.replaceAll('\n* ', '</li><li>');
if (markdown.match('\n')) {
markdown = markdown.replace('\n', '</li></ul><p>');
markdown = markdown + '</p>';
} else {
markdown = markdown + '</li></ul>';
}
} else {
markdown = markdown + '</h1>';
}
} else if (markdown.startsWith('## ')) {
markdown = markdown.replace('## ', '<h2>') + '</h2>';
} else if (markdown.startsWith('### ')) {
markdown = markdown.replace('### ', '<h3>') + '</h3>';
} else if (markdown.startsWith('#### ')) {
markdown = markdown.replace('#### ', '<h4>') + '</h4>';
} else if (markdown.startsWith('##### ')) {
markdown = markdown.replace('##### ', '<h5>') + '</h5>';
} else if (markdown.startsWith('###### ')) {
markdown = markdown.replace('###### ', '<h6>') + '</h6>';
} else if (markdown.startsWith('* ')) {
markdown = markdown.replace('* ', '<ul><li>');
markdown = markdown.replaceAll('\n* ', '</li><li>');
markdown = markdown + '</li></ul>';
} else {
markdown = `<p>${markdown}</p>`;
}

markdown = markdown.replace('\n* ', '<ul><li>');
markdown = markdown.replaceAll('\n* ', '</li><li>');
markdown = markdown.replace('\n', '<ul><li>');

return markdown;
};
66 changes: 66 additions & 0 deletions exercises/practice/markdown/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[e75c8103-a6b8-45d9-84ad-e68520545f6e]
description = "parses normal text as a paragraph"

[69a4165d-9bf8-4dd7-bfdc-536eaca80a6a]
description = "parsing italics"

[ec345a1d-db20-4569-a81a-172fe0cad8a1]
description = "parsing bold text"

[51164ed4-5641-4909-8fab-fbaa9d37d5a8]
description = "mixed normal, italics and bold text"

[ad85f60d-0edd-4c6a-a9b1-73e1c4790d15]
description = "with h1 header level"

[d0f7a31f-6935-44ac-8a9a-1e8ab16af77f]
description = "with h2 header level"

[9df3f500-0622-4696-81a7-d5babd9b5f49]
description = "with h3 header level"

[50862777-a5e8-42e9-a3b8-4ba6fcd0ed03]
description = "with h4 header level"

[ee1c23ac-4c86-4f2a-8b9c-403548d4ab82]
description = "with h5 header level"

[13b5f410-33f5-44f0-a6a7-cfd4ab74b5d5]
description = "with h6 header level"

[6dca5d10-5c22-4e2a-ac2b-bd6f21e61939]
description = "with h7 header level"
include = false

[81c0c4db-435e-4d77-860d-45afacdad810]
description = "h7 header level is a paragraph"
reimplements = "6dca5d10-5c22-4e2a-ac2b-bd6f21e61939"

[25288a2b-8edc-45db-84cf-0b6c6ee034d6]
description = "unordered lists"

[7bf92413-df8f-4de8-9184-b724f363c3da]
description = "With a little bit of everything"

[0b3ed1ec-3991-4b8b-8518-5cb73d4a64fe]
description = "with markdown symbols in the header text that should not be interpreted"

[113a2e58-78de-4efa-90e9-20972224d759]
description = "with markdown symbols in the list item text that should not be interpreted"

[e65e46e2-17b7-4216-b3ac-f44a1b9bcdb4]
description = "with markdown symbols in the paragraph text that should not be interpreted"

[f0bbbbde-0f52-4c0c-99ec-be4c60126dd4]
description = "unordered lists close properly with preceding and following lines"
1 change: 1 addition & 0 deletions exercises/practice/markdown/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/markdown/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions exercises/practice/markdown/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
useBuiltIns: 'entry',
corejs: '3.19',
},
],
],
plugins: ['@babel/plugin-syntax-bigint'],
};
72 changes: 72 additions & 0 deletions exercises/practice/markdown/markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// This is only a SKELETON file for the 'Matrix' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const parse = (markdown) => {
if (markdown.includes(' __')) {
markdown = markdown.replace(' __', ' <strong>');
}
if (markdown.includes(' _')) {
markdown = markdown.replace(' _', ' <em>');
}

if (markdown.includes('__ ')) {
markdown = markdown.replace('__ ', '</strong> ');
}
if (markdown.includes('_ ')) {
markdown = markdown.replace('_ ', '</em> ');
}

if (markdown.match(/__\w/g)) {
markdown = markdown.replace('__', '<strong>');
}
if (markdown.match(/\w__/g)) {
markdown = markdown.replace('__', '</strong>');
}

if (markdown.match(/_\w/g)) {
markdown = markdown.replace('_', '<em>');
}
if (markdown.match(/\w_/g)) {
markdown = markdown.replace('_', '</em>');
}

if (markdown.startsWith('# ')) {
markdown = markdown.replace('# ', '<h1>');
if (markdown.match(/\n\*/gm)) {
markdown = markdown.replace('\n* ', '</h1><ul><li>');
markdown = markdown.replaceAll('\n* ', '</li><li>');
if (markdown.match('\n')) {
markdown = markdown.replace('\n', '</li></ul><p>');
markdown = markdown + '</p>';
} else {
markdown = markdown + '</li></ul>';
}
} else {
markdown = markdown + '</h1>';
}
} else if (markdown.startsWith('## ')) {
markdown = markdown.replace('## ', '<h2>') + '</h2>';
} else if (markdown.startsWith('### ')) {
markdown = markdown.replace('### ', '<h3>') + '</h3>';
} else if (markdown.startsWith('#### ')) {
markdown = markdown.replace('#### ', '<h4>') + '</h4>';
} else if (markdown.startsWith('##### ')) {
markdown = markdown.replace('##### ', '<h5>') + '</h5>';
} else if (markdown.startsWith('###### ')) {
markdown = markdown.replace('###### ', '<h6>') + '</h6>';
} else if (markdown.startsWith('* ')) {
markdown = markdown.replace('* ', '<ul><li>');
markdown = markdown.replaceAll('\n* ', '</li><li>');
markdown = markdown + '</li></ul>';
} else {
markdown = `<p>${markdown}</p>`;
}

markdown = markdown.replace('\n* ', '<ul><li>');
markdown = markdown.replaceAll('\n* ', '</li><li>');
markdown = markdown.replace('\n', '<ul><li>');

return markdown;
};
83 changes: 83 additions & 0 deletions exercises/practice/markdown/markdown.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { parse } from './markdown';

describe('Markdown', () => {
test('test parses normal text as a paragraph', () => {
const markdown = 'This will be a paragraph';
expect(parse(markdown)).toMatch('<p>This will be a paragraph</p>');
});

xtest('test parsing italics', () => {
const markdown = '_This will be italic_';
expect(parse(markdown)).toMatch('<p><em>This will be italic</em></p>');
});

xtest('test parsing bold text', () => {
const markdown = '__This will be bold__';
expect(parse(markdown)).toMatch(
'<p><strong>This will be bold</strong></p>'
);
});

xtest('test mixed normal italics and bold text', () => {
const markdown = 'This will _be_ __mixed__';
expect(parse(markdown)).toMatch(
'<p>This will <em>be</em> <strong>mixed</strong></p>'
);
});

xtest('test with h1 header level', () => {
const markdown = '# This will be an h1';
expect(parse(markdown)).toMatch('<h1>This will be an h1</h1>');
});

xtest('test with h2 header level', () => {
const markdown = '## This will be an h2';
expect(parse(markdown)).toMatch('<h2>This will be an h2</h2>');
});

xtest('test with h6 header level', () => {
const markdown = '###### This will be an h6';
expect(parse(markdown)).toMatch('<h6>This will be an h6</h6>');
});

xtest('test unordered lists', () => {
const markdown = '* Item 1\n* Item 2';
expect(parse(markdown)).toMatch('<ul><li>Item 1</li><li>Item 2</li></ul>');
});

xtest('test with a little bit of everything', () => {
const markdown = '# Header!\n* __Bold Item__\n* _Italic Item_';
expect(parse(markdown)).toMatch(
'<h1>Header!</h1><ul><li><strong>Bold Item</strong></li><li><em>Italic Item</em></li></ul>'
);
});

xtest('test with markdown symbols in the header text that should not be interpreted', () => {
const markdown = '# This is a header with # and * in the text';
expect(parse(markdown)).toMatch(
'<h1>This is a header with # and * in the text</h1>'
);
});

xtest('test with markdown symbols in the list item text that should not be interpreted', () => {
const markdown =
'* Item 1 with a # in the text\n* Item 2 with * in the text';
expect(parse(markdown)).toMatch(
'<ul><li>Item 1 with a # in the text</li><li>Item 2 with * in the text</li></ul>'
);
});

xtest('test with markdown symbols in the paragraph text that should not be interpreted', () => {
const markdown = 'This is a paragraph with # and * in the text';
expect(parse(markdown)).toMatch(
'<p>This is a paragraph with # and * in the text</p>'
);
});

xtest('test unordered lists close properly with preceding and following lines', () => {
const markdown = '# Start a list\n* Item 1\n* Item 2\nEnd a list';
expect(parse(markdown)).toMatch(
'<h1>Start a list</h1><ul><li>Item 1</li><li>Item 2</li></ul><p>End a list</p>'
);
});
});
Loading