Skip to content

Commit

Permalink
Merge pull request #54 from unexpectedjs/evaldown
Browse files Browse the repository at this point in the history
Port to evaldown
  • Loading branch information
alexjeffburke committed Jul 4, 2020
2 parents 1c7c88d + cf67f05 commit d997e84
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 1,813 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: node_js
env:
- TARGET=test
node_js:
- '8'
- '10'
- '12'
- 'node'
Expand All @@ -13,7 +12,7 @@ matrix:
env:
- TARGET=lint
- name: Coverage
node_js: '8'
node_js: '10'
env:
- TARGET=coverage
script: npm run $TARGET
Expand Down
3 changes: 3 additions & 0 deletions bootstrap-unexpected-markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* global expect:true */
expect = require('unexpected').clone();
expect.output.preferredWidth = 80;
51 changes: 0 additions & 51 deletions lib/Snippets.js

This file was deleted.

102 changes: 0 additions & 102 deletions lib/UnexpectedMarkdown.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
var Snippets = require('./Snippets');
var createExpect = require('./createExpect');
var snippetRegexp = require('./snippetRegexp');
var marked = require('marked-papandreou');
var fs = require('fs');
var pathModule = require('path');
var convertMarkdownToMocha = require('./convertMarkdownToMocha');
var cleanStackTrace = require('./cleanStackTrace');

var maps = {};

Expand Down Expand Up @@ -45,100 +40,3 @@ if (sourceMapSupport) {
},
});
}

var styleRegex = /style=".*?"/;

class UnexpectedMarkdown {
constructor(content) {
if (!(this instanceof UnexpectedMarkdown)) {
return new UnexpectedMarkdown(content);
}
this.content = content;
}

async toHtml(options) {
var content = this.content;
var expect = createExpect(options);

const snippets = await this.getSnippets(options);

var index = 0;
var renderer = new marked.Renderer();
renderer.code = function (code) {
var snippet = snippets.get(index);
var previousSnippet = snippets.get(index - 1);
index += 1;
var lang = snippet.lang;
if (lang === 'output') {
if (previousSnippet && previousSnippet.lang === 'javascript') {
if (previousSnippet.htmlErrorMessage) {
return previousSnippet.htmlErrorMessage.replace(
styleRegex,
'class="output"'
);
}

return '<div class="output"></div>';
} else {
throw new Error(
`No matching javascript block for output:\n${snippet.code}`
);
}
}

return expect.output
.clone('html')
.code(code, lang)
.toString()
.replace(styleRegex, `class="code ${this.options.langPrefix}${lang}"`);
};

return marked(content, { renderer: renderer, ...options });
}

async getSnippets(options) {
if (this.snippets) {
return this.snippets;
} else {
return Snippets.fromMarkdown(this.content).evaluate(options);
}
}

async withUpdatedExamples(options) {
const snippets = await this.getSnippets(options);

var index = 0;
var updatedContent = this.content.replace(snippetRegexp, function (
$0,
htmlComments,
lang
) {
var currentIndex = index;
index += 1;
var snippet = snippets.get(currentIndex);
if (snippet.lang === 'output') {
var example = snippets.get(currentIndex - 1);
var output = '';
if (example && example.lang === 'javascript') {
if (example.errorMessage) {
output = example.errorMessage;
if (snippet.flags.cleanStackTrace) {
output = cleanStackTrace(output);
}
}
}
return `${htmlComments || ''}\`\`\`${lang}\n${output}\n\`\`\``;
} else {
return $0;
}
});

return new UnexpectedMarkdown(updatedContent);
}

toString() {
return this.content;
}
}

module.exports = UnexpectedMarkdown;

0 comments on commit d997e84

Please sign in to comment.