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

fix(gatsby-remark-prismjs): Handle diff-language styling issue #27659

Merged
merged 3 commits into from
Nov 2, 2020

Conversation

SeokminHong
Copy link
Contributor

Description

PR #26525 provides the awesome functionality of handling syntax highlighting during diff highlighting. However, it does not handle all cases of, the language not been loaded yet. I fixed a test and snapshot for diff-highlighting to reproduce the bug.

When you applied the commit a2ecf98, it fails the test.

// gatsby-remark-prismjs/src/__tests__/highlight-code.js
- it(`for language diff-js`, () => {
+ it(`for language diff-typescript`, () => {
    const highlightCode = require(`../highlight-code`)
    const language = `diff`
-   const diffLanguage = `js`
+   const diffLanguage = `typescript`
    ...

    expect(
      highlightCode(
        language,
        code,
        {},
        lineNumbersHighlight,
        false,
        diffLanguage
      )
    ).toMatchSnapshot()
  })
yarn jest packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js 
yarn run v1.21.0
$ jest packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js
 FAIL  packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js
  highlight code and lines with PrismJS
    ✓ for language cpp (26ms)
    ✓ for language jsx (28ms)
    ✕ for language diff-typescript (44ms)
    with language-text
      ✓ escapes &, <, " elements and warns (5ms)
      ✓ can warn about languages missing from inline code (4ms)
      ✓ warns once per language (5ms)
    with language-none
      ✓ does not escape its contents (4ms)
    with non-highlight-lines
      ✓ does not add trailing newlines (4ms)
      ✓ a trailing newline is preserved (3ms)
      ✓ does not add trailing newlines (3ms)
      ✓ a trailing newline is preserved (4ms)

  ● highlight code and lines with PrismJS › for language diff-typescript

    expect(received).toMatchSnapshot()

    Snapshot name: `highlight code and lines with PrismJS for language diff-typescript 1`

    - Snapshot
    + Received

      ↵
    - <span class="token deleted-sign deleted language-typescript"><span class="token prefix deleted">-</span>    <span class="token keyword">let</span> foo <span class="token operator">=</span> bar<span class="token punctuation">.</span><span class="token function">baz</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    - <span class="token prefix deleted">-</span>    foo <span class="token operator">=</span> foo <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    + <span class="token deleted-sign deleted"><span class="token prefix deleted">-</span>    let foo = bar.baz([1, 2, 3]);
    + <span class="token prefix deleted">-</span>    foo = foo + 1;
    - </span><span class="token inserted-sign inserted language-typescript"><span class="token prefix inserted">+</span>    <span class="token keyword">const</span> foo <span class="token operator">=</span> bar<span class="token punctuation">.</span><span class="token function">baz</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    + </span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span>    const foo = bar.baz([1, 2, 3]) + 1;
    - </span><span class="token unchanged language-typescript"><span class="token prefix unchanged"> </span>    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    + </span><span class="token unchanged"><span class="token prefix unchanged"> </span>    console.log(foo);
      </span>

      77 |         diffLanguage
      78 |       )
    > 79 |     ).toMatchSnapshot()
         |       ^
      80 |   })
      81 | 
      82 |   describe(`with language-text`, () => {

      at Object.<anonymous> (packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js:79:7)

We can see the syntax highlighting is not applying to the newer test. Now, try the test after apply the following change.

// gatsby-remark-prismjs/src/__tests__/highlight-code.js
  it(`for language diff-typescript`, () => {
    const highlightCode = require(`../highlight-code`)
    const language = `diff`
    const diffLanguage = `typescript`
+  // pre-load typescript
+   highlightCode(diffLanguage, ``)
    ...

    expect(
      highlightCode(
        language,
        code,
        {},
        lineNumbersHighlight,
        false,
        diffLanguage
      )
    ).toMatchSnapshot()
  })

Now the test passes. We can figure out it only is styled when the diff-language is already loaded because "highlightCode" doesn't load the diff-language.

Therefore, I added the statements calling load diff-language if it is possible. Also, I removed the duplication-checking statement (if (!Prism.languages[language]) because the loadPrismLanguage function already does itself.

@SeokminHong SeokminHong requested a review from a team as a code owner October 26, 2020 07:27
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 26, 2020
@LekoArts LekoArts added topic: remark/mdx Related to Markdown, remark & MDX ecosystem and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Oct 26, 2020
@pieh pieh self-assigned this Oct 30, 2020
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Thanks!

@pieh pieh merged commit a33e7fb into gatsbyjs:master Nov 2, 2020
@pieh
Copy link
Contributor

pieh commented Nov 2, 2020

Published gatsby-remark-prismjs@3.6.0 with this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: remark/mdx Related to Markdown, remark & MDX ecosystem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants