Skip to content

Unfork one file, and prepare for unforking more #5

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-env'],
};
207 changes: 0 additions & 207 deletions fenced_code.js

This file was deleted.

5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
transform: {
'.\\.js$': 'babel-jest',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm still getting the following error when we start using code from @zulip/shared in the next commit:

 FAIL  __tests__/preview-tests.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/chrisbobbe/dev/zulip-markdown-parser/node_modules/@zulip/shared/js/fenced_code.js:1
    import katex from "katex";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      14 | const marked = require('./marked');
      15 | const hash_util = require('./hash_util');
    > 16 | const fenced_code = require('@zulip/shared/js/fenced_code.js');
         |                     ^
      17 | const emoji = require('./emoji');
      18 | var emoji_codes = require('./emoji_codes');
      19 | const katex = require('katex');

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1179:56)
      at Object.<anonymous> (markdown.js:16:21)
      at Object.<anonymous> (parseMarkdown.js:1:18)
      at Object.<anonymous> (__tests__/preview-tests.js:3:23)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It goes away when I apply the following to jest.config.js (like we do in zulip-mobile):

+ const transformModulesWhitelist = ['@zulip/shared'];
+
+ const transformIgnorePattern = `node_modules/(?!${transformModulesWhitelist.join('|')})`;
+
  module.exports = {
    transform: {
      '.\\.js$': 'babel-jest',
    },
+   transformIgnorePatterns: [transformIgnorePattern],
  };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the first item in that error's list of possible solutions—

     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.

—it seems like anything in node_modules won't be transformed by default.

},
};
3 changes: 1 addition & 2 deletions markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var find = require('lodash.find');
var contains = require('lodash.contains');
const marked = require('./marked');
const hash_util = require('./hash_util');
const fenced_code = require('./fenced_code');
const fenced_code = require('@zulip/shared/js/fenced_code.js');
const emoji = require('./emoji');
var emoji_codes = require('./emoji_codes');
const katex = require('katex');
Expand Down Expand Up @@ -316,7 +316,6 @@ exports.initialize = function (people, stream_data, realm, realm_filters, realm_
fenced_code.set_stash_func(function (html) {
return marked.stashHtml(html, true);
});
fenced_code.set_escape_func(escape);

marked.setOptions({
gfm: true,
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^24.9.0"
"@babel/core": "^7.11.4",
"@babel/preset-env": "^7.11.0",
"babel-jest": "^26.3.0",
"jest": "^26.4.2"
},
"dependencies": {
"katex": "^0.7.1",
"@zulip/shared": "^0.0.3",
"katex": "^0.12.0",
"lodash.contains": "^2.4.3",
"lodash.find": "^4.6.0",
"lodash.foreach": "^4.5.0",
Expand Down
30 changes: 15 additions & 15 deletions test_cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ exports.testCases = [
{
input: "\n```\nfenced code\n```\n\nand then after\n",
expected:
'<div class="codehilite"><pre><span></span>fenced code\n</pre></div>\n\n\n<p>and then after</p>'
'<div class="codehilite"><pre><span></span><code>fenced code\n</code></pre></div>\n\n\n<p>and then after</p>'
},
{
input:
"\n```\n fenced code trailing whitespace \n```\n\nand then after\n",
expected:
'<div class="codehilite"><pre><span></span> fenced code trailing whitespace\n</pre></div>\n\n\n<p>and then after</p>'
'<div class="codehilite"><pre><span></span><code> fenced code trailing whitespace\n</code></pre></div>\n\n\n<p>and then after</p>'
},
{
input: "* a\n* list \n* here",
Expand All @@ -30,12 +30,12 @@ exports.testCases = [
{
input: "\n```c#\nfenced code special\n```\n\nand then after\n",
expected:
'<div class="codehilite"><pre><span></span>fenced code special\n</pre></div>\n\n\n<p>and then after</p>'
'<div class="codehilite"><pre><span></span><code>fenced code special\n</code></pre></div>\n\n\n<p>and then after</p>'
},
{
input: "\n```vb.net\nfenced code dot\n```\n\nand then after\n",
expected:
'<div class="codehilite"><pre><span></span>fenced code dot\n</pre></div>\n\n\n<p>and then after</p>'
'<div class="codehilite"><pre><span></span><code>fenced code dot\n</code></pre></div>\n\n\n<p>and then after</p>'
},
{
input: "Some text first\n* a\n* list \n* here\n\nand then after",
Expand Down Expand Up @@ -130,8 +130,8 @@ exports.testCases = [
{
"name": "codeblock_hilite",
"input": "Hamlet said:\n~~~~.python \ndef speak(self):\n x = 1\n~~~~",
"expected_output": `<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">speak</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n <span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>\n</pre></div>`,
"marked_expected_output": "<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span>def speak(self):\n x = 1\n</pre></div>"
"expected_output": `<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span><code><span class=\"k\">def</span> <span class=\"nf\">speak</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n <span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>\n</code></pre></div>`,
"marked_expected_output": "<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span><code>def speak(self):\n x = 1\n</code></pre></div>"
},
{
"name": "basic_paragraph",
Expand All @@ -142,25 +142,25 @@ exports.testCases = [
{
"name": "codeblock_multiline",
"input": "Hamlet once said\n~~~~\ndef func():\n x = 1\n\n y = 2\n\n z = 3\n~~~~\nAnd all was good.",
"expected_output": `<p>Hamlet once said</p>\n<div class=\"codehilite\"><pre><span></span>def func():\n x = 1\n\n y = 2\n\n z = 3\n</pre></div>\n\n\n<p>And all was good.</p>`,
"expected_output": `<p>Hamlet once said</p>\n<div class=\"codehilite\"><pre><span></span><code>def func():\n x = 1\n\n y = 2\n\n z = 3\n</code></pre></div>\n\n\n<p>And all was good.</p>`,
"bugdown_matches_marked": true
},
{
"name": "codeblock_trailing_whitespace",
"input": "Hamlet once said\n~~~~\ndef func():\n x = 1\n\n y = 2\t\t\n\n z = 3 \n~~~~\nAnd all was good.",
"expected_output": `<p>Hamlet once said</p>\n<div class=\"codehilite\"><pre><span></span>def func():\n x = 1\n\n y = 2\n\n z = 3\n</pre></div>\n\n\n<p>And all was good.</p>`,
"expected_output": `<p>Hamlet once said</p>\n<div class=\"codehilite\"><pre><span></span><code>def func():\n x = 1\n\n y = 2\n\n z = 3\n</code></pre></div>\n\n\n<p>And all was good.</p>`,
"bugdown_matches_marked": true
},
{
"name": "codeblock_backticks",
"input": "\n```\nfenced code\n```\n\n```inline code```\n",
"expected_output": `<div class=\"codehilite\"><pre><span></span>fenced code\n</pre></div>\n\n\n<p><code>inline code</code></p>`,
"expected_output": `<div class=\"codehilite\"><pre><span></span><code>fenced code\n</code></pre></div>\n\n\n<p><code>inline code</code></p>`,
"bugdown_matches_marked": true
},
{
"name": "hanging_multi_codeblock",
"input": "Hamlet said:\n~~~~\ndef speak(self):\n x = 1\n# Comment to make this code block longer to test Trac #1162\n~~~~\n\nThen he mentioned ````y = 4 + x**2```` and\n~~~~\ndef foobar(self):\n return self.baz()",
"expected_output": `<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span>def speak(self):\n x = 1\n# Comment to make this code block longer to test Trac #1162\n</pre></div>\n\n\n<p>Then he mentioned <code>y = 4 + x**2</code> and</p>\n<div class=\"codehilite\"><pre><span></span>def foobar(self):\n return self.baz()\n</pre></div>`,
"expected_output": `<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span><code>def speak(self):\n x = 1\n# Comment to make this code block longer to test Trac #1162\n</code></pre></div>\n\n\n<p>Then he mentioned <code>y = 4 + x**2</code> and</p>\n<div class=\"codehilite\"><pre><span></span><code>def foobar(self):\n return self.baz()\n</code></pre></div>`,
"bugdown_matches_marked": true
},
{
Expand All @@ -178,7 +178,7 @@ exports.testCases = [
{
"name": "complexly_nested_quote",
"input": "I heard about this second hand...\n~~~ quote\n\nHe said:\n~~~ quote\nThe customer is complaining.\n\nThey looked at this code:\n``` \ndef hello(): print 'hello\n```\nThey would prefer:\n~~~\ndef hello()\n puts 'hello'\nend\n~~~\n\nPlease advise.\n~~~\nShe said:\n~~~ quote\nJust send them this:\n```\necho \"hello\n\"\n```\n~~~",
"expected_output": `<p>I heard about this second hand...</p>\n<blockquote>\n<p>He said:</p>\n<blockquote>\n<p>The customer is complaining.</p>\n<p>They looked at this code:</p>\n<div class=\"codehilite\"><pre><span></span>def hello(): print &#39;hello\n</pre></div>\n\n\n<p>They would prefer:</p>\n</blockquote>\n<p>def hello()<br>\n puts 'hello'<br>\nend</p>\n</blockquote>\n<p>Please advise.</p>\n<div class=\"codehilite\"><pre><span></span>She said:\n~~~ quote\nJust send them this:\n\`\`\`\necho &quot;hello\n&quot;\n\`\`\`\n</pre></div>`,
"expected_output": `<p>I heard about this second hand...</p>\n<blockquote>\n<p>He said:</p>\n<blockquote>\n<p>The customer is complaining.</p>\n<p>They looked at this code:</p>\n<div class=\"codehilite\"><pre><span></span><code>def hello(): print &#39;hello\n</code></pre></div>\n\n\n<p>They would prefer:</p>\n</blockquote>\n<p>def hello()<br>\n puts 'hello'<br>\nend</p>\n</blockquote>\n<p>Please advise.</p>\n<div class=\"codehilite\"><pre><span></span><code>She said:\n~~~ quote\nJust send them this:\n\`\`\`\necho &quot;hello\n&quot;\n\`\`\`\n</code></pre></div>`,
"bugdown_matches_marked": false
},
{
Expand All @@ -196,13 +196,13 @@ exports.testCases = [
{
"name": "dangerous_block",
"input": "``` one ```\n\n``` two ```\n\n~~~~\nx = 1",
"expected_output": `<p><code>one</code></p>\n<p><code>two</code></p>\n<div class=\"codehilite\"><pre><span></span>x = 1\n</pre></div>`,
"expected_output": `<p><code>one</code></p>\n<p><code>two</code></p>\n<div class=\"codehilite\"><pre><span></span><code>x = 1\n</code></pre></div>`,
"bugdown_matches_marked": true
},
{
"name": "four_space_code_block",
"input": " def foo(): pass",
"expected_output": `<div class=\"codehilite\"><pre><span></span>def foo(): pass\n</pre></div>`,
"expected_output": `<div class=\"codehilite\"><pre><span></span><code>def foo(): pass\n</code></pre></div>`,
"marked_expected_output": "<div class=\"codehilite\"><pre>def foo(): pass\n</pre></div>"
},
{
Expand Down Expand Up @@ -251,7 +251,7 @@ exports.testCases = [
{
"name": "ulist_codeblock",
"input": "~~~\nint x = 3\n* 4;\n~~~",
"expected_output": `<div class=\"codehilite\"><pre><span></span>int x = 3\n* 4;\n</pre></div>`,
"expected_output": `<div class=\"codehilite\"><pre><span></span><code>int x = 3\n* 4;\n</code></pre></div>`,
"bugdown_matches_marked": true
},
{
Expand Down Expand Up @@ -515,7 +515,7 @@ exports.testCases = [
{
"name": "safe_html_in_code",
"input": "~~~\n<h1>stay normal</h1>",
"expected_output": `<div class=\"codehilite\"><pre><span></span>&lt;h1&gt;stay normal&lt;/h1&gt;\n</pre></div>`,
"expected_output": `<div class=\"codehilite\"><pre><span></span><code>&lt;h1&gt;stay normal&lt;/h1&gt;\n</code></pre></div>`,
"bugdown_matches_marked": true
},
{
Expand Down
Loading