Skip to content

Commit

Permalink
fix: improves regex for ascii and test (#461)
Browse files Browse the repository at this point in the history
fix: file name in unit test

fix: updates snapshots

fix: updates snapshots
  • Loading branch information
ayusharma committed Jun 23, 2018
1 parent 50db21c commit be3968f
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,11 @@ function addGravatarSupport(pkgInfo: any) {
* @return {String} converted html template
*/
function parseReadme(packageName: string, readme: string): string {
const ascii = asciidoctor();
const docTypeIdentifier = new RegExp(/^=+ \w/, 'g');

const asciiRegex = /^\n?(?:={1,5}[ \t]+\S|[^#].*(\n(?!#+[ \t]+\S).*){0,8}\n={1,5}[ \t]+\S)/;
const docTypeIdentifier = new RegExp(asciiRegex, 'g');
// asciidoc
if (docTypeIdentifier.test(readme)) {
const ascii = asciidoctor();
return ascii.convert(readme, {safe: 'safe', attributes: {showtitle: true, icons: 'font'}});
}

Expand Down
78 changes: 78 additions & 0 deletions test/unit/__snapshots__/utils.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Utilities parseReadme should pass for ascii/makrdown text to html template 1`] = `
"<h1 id=\\"project-title\\">Project Title</h1>
<p>One Paragraph of project description goes here</p>
<h2 id=\\"getting-started\\">Getting Started</h2>
<p>These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.</p>
<h3 id=\\"prerequisites\\">Prerequisites</h3>
<p>What things you need to install the software and how to install them</p>
<pre><code>Give examples
</code></pre><h3 id=\\"installing\\">Installing</h3>
<p>A step by step series of examples that tell you how to get a development env running</p>
<p>Say what the step will be</p>
<pre><code>Give the example
</code></pre><p>And repeat</p>
<pre><code>until finished
</code></pre><p>End with an example of getting some data out of the system or using it for a little demo</p>
<h2 id=\\"running-the-tests\\">Running the tests</h2>
<p>Explain how to run the automated tests for this system</p>
<h3 id=\\"break-down-into-end-to-end-tests\\">Break down into end to end tests</h3>
<p>Explain what these tests test and why</p>
<pre><code>Give an example
</code></pre><h3 id=\\"and-coding-style-tests\\">And coding style tests</h3>
<p>Explain what these tests test and why</p>
<pre><code>Give an example
</code></pre><h2 id=\\"deployment\\">Deployment</h2>
<p>Add additional notes about how to deploy this on a live system</p>
<h2 id=\\"built-with\\">Built With</h2>
<ul>
<li>The web framework used</li>
<li>Dependency Management</li>
<li>Used to generate RSS Feeds</li>
</ul>
<h2 id=\\"contributing\\">Contributing</h2>
<p>Please read <a href=\\"CONTRIBUTING.md\\">CONTRIBUTING.md</a> for details on our code of conduct, and the process for submitting pull requests to us.</p>
<h2 id=\\"versioning\\">Versioning</h2>
<p>We use <a href=\\"http://semver.org/\\">SemVer</a> for versioning. For the versions available, see the <a href=\\"https://github.com/your/project/tags\\">tags on this repository</a>.</p>
<h2 id=\\"license\\">License</h2>
<p>This project is licensed under the MIT License - see the <a href=\\"LICENSE.md\\">LICENSE.md</a> file for details</p>
<h2 id=\\"acknowledgments\\">Acknowledgments</h2>
<ul>
<li>Hat tip to anyone whose code was used</li>
<li>Inspiration</li>
<li>etc</li>
</ul>
"
`;
exports[`Utilities parseReadme should pass for ascii/makrdown text to html template 2`] = `
"<h1>Hello, AsciiDoc!</h1>
<div id=\\"preamble\\">
<div class=\\"sectionbody\\">
<div class=\\"paragraph\\">
<p>An introduction to <a href=\\"http://asciidoc.org\\">AsciiDoc</a>.</p>
</div>
</div>
</div>
<div class=\\"sect1\\">
<h2 id=\\"_first_section\\">First Section</h2>
<div class=\\"sectionbody\\">
<div class=\\"ulist\\">
<ul>
<li>
<p>item 1</p>
</li>
<li>
<p>item 2</p>
</li>
</ul>
</div>
<div class=\\"listingblock\\">
<div class=\\"content\\">
<pre class=\\"highlight\\"><code class=\\"language-ruby\\" data-lang=\\"ruby\\">puts \\"Hello, World!\\"</code></pre>
</div>
</div>
</div>
</div>"
`;
12 changes: 12 additions & 0 deletions test/unit/partials/readme/ascii.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
= Hello, AsciiDoc!
Doc Writer <doc@example.com>

An introduction to http://asciidoc.org[AsciiDoc].

== First Section

* item 1
* item 2

[source,ruby]
puts "Hello, World!"
81 changes: 81 additions & 0 deletions test/unit/partials/readme/markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Project Title

One Paragraph of project description goes here

## Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

### Prerequisites

What things you need to install the software and how to install them

```
Give examples
```

### Installing

A step by step series of examples that tell you how to get a development env running

Say what the step will be

```
Give the example
```

And repeat

```
until finished
```

End with an example of getting some data out of the system or using it for a little demo

## Running the tests

Explain how to run the automated tests for this system

### Break down into end to end tests

Explain what these tests test and why

```
Give an example
```

### And coding style tests

Explain what these tests test and why

```
Give an example
```

## Deployment

Add additional notes about how to deploy this on a live system

## Built With

* The web framework used
* Dependency Management
* Used to generate RSS Feeds

## Contributing

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags).

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

## Acknowledgments

* Hat tip to anyone whose code was used
* Inspiration
* etc
9 changes: 8 additions & 1 deletion test/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import assert from 'assert';
import {validateName as validate, convertDistRemoteToLocalTarballUrls, parseReadme} from '../../src/lib/utils';
import {generateGravatarUrl, GRAVATAR_DEFAULT} from '../../src/utils/user';
import {spliceURL} from '../../src/utils/string';
import Package from "../../src/webui/src/components/Package";
import Package from '../../src/webui/src/components/Package';
import Logger, {setup} from '../../src/lib/logger';
import { readFile } from '../functional/lib/test.utils';

const readmeFile = (fileName: string = 'markdown.md') => readFile(`../../unit/partials/readme/${fileName}`);

setup([]);

Expand Down Expand Up @@ -118,15 +121,19 @@ describe('Utilities', () => {
test('should pass for ascii/makrdown text to html template', () => {
const markdown = '# markdown';
const ascii = "= AsciiDoc";

expect(parseReadme('testPackage', markdown)).toEqual('<h1 id="markdown">markdown</h1>\n');
expect(parseReadme('testPackage', ascii)).toEqual('<h1>AsciiDoc</h1>\n');
expect(parseReadme('testPackage', String(readmeFile('markdown.md')))).toMatchSnapshot();
expect(parseReadme('testPackage', String(readmeFile('ascii.adoc')))).toMatchSnapshot();
});

test('should pass for conversion of non-ascii to markdown text', () => {
const simpleText = 'simple text';
const randomText = '%%%%%**##==';
const randomTextNonAscii = 'simple text \n = ascii';
const randomTextMarkdown = 'simple text \n # markdown';

expect(parseReadme('testPackage', randomText)).toEqual('<p>%%%%%**##==</p>\n');
expect(parseReadme('testPackage', simpleText)).toEqual('<p>simple text</p>\n');
expect(parseReadme('testPackage', randomTextNonAscii))
Expand Down

0 comments on commit be3968f

Please sign in to comment.