Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Apr 9, 2024
2 parents 5e0af8f + dafabfc commit d88f3bf
Show file tree
Hide file tree
Showing 27 changed files with 203 additions and 431 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/node_modules
coverage
dist
example/index*
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
strategy:
matrix:
node-version: [18.x, 20.x, 21.x]
react: [16, 17, 18]
steps:
- uses: actions/checkout@v3

Expand All @@ -22,6 +23,10 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Install React <18 deps
if: matrix.react == '16' || matrix.react == '17'
run: npm i react@${{ matrix.react }} react-dom@${{ matrix.react }} @testing-library/react@12

- name: Run tests
run: npm test

Expand All @@ -31,7 +36,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x, 21.x]
node-version: [20.x]
react: [18]

steps:
- uses: actions/checkout@v3
Expand Down
20 changes: 8 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
ARG NODE_VERSION=18
FROM node:${NODE_VERSION}-buster
FROM node:${NODE_VERSION}-alpine3.18

ARG NODE_VERSION
ENV NODE_VERSION=$NODE_VERSION

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable

RUN apt-get update && apt-get install -y \
curl \
gnupg \
fonts-noto-color-emoji \
&& curl --location --silent https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install google-chrome-stable -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium-browser

RUN apk update && apk add \
make \
font-noto-emoji \
font-roboto \
chromium

RUN npm install -g npm@latest

Expand Down
40 changes: 40 additions & 0 deletions Dockerfile.legacy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ARG NODE_VERSION=18
FROM node:${NODE_VERSION}-alpine3.18

ARG NODE_VERSION
ENV NODE_VERSION=$NODE_VERSION

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium-browser

ENV USE_LEGACY=true

ARG REACT_VERSION
ENV REACT_VERSION=$REACT_VERSION

RUN apk update && apk add \
make \
font-noto-emoji \
font-roboto \
chromium

RUN npm install -g npm@latest

ENV DOCKER_WORKSPACE=/markdown
WORKDIR ${DOCKER_WORKSPACE}

COPY package.json package-lock.json ./
RUN npm install

RUN npm install react@${REACT_VERSION} react-dom@${REACT_VERSION} @testing-library/react@12

COPY . ./

RUN mkdir -p __tests__/browser/__image_snapshots__/__diff_output__

RUN make emojis

EXPOSE 9966

CMD ["test.browser"]
ENTRYPOINT ["npm", "run"]
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ example/public/img/emojis: node_modules/@readme/emojis
mkdir -p example/public/img/emojis
cp node_modules/@readme/emojis/src/img/*.png example/public/img/emojis/

ifeq ($(USE_LEGACY), true)
dockerfile = -f Dockerfile.legacy
endif

build:
docker build --platform linux/amd64 -t markdown .
@echo USE_LEGACY=$(USE_LEGACY)
docker build --platform linux/amd64 -t markdown $(dockerfile) --build-arg REACT_VERSION=${REACT_VERSION} .

# This lets us call `make run test.browser`. Make expects cmdline args
# to be targets. So this creates noop targets out of args. Copied from
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 6 additions & 9 deletions __tests__/components/HTMLBlock.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { render, screen } from '@testing-library/react';
import React from 'react';
import { renderToString } from 'react-dom/server';

import createHTMLBlock from '../../components/HTMLBlock';
import { react } from '../../index';
Expand Down Expand Up @@ -46,11 +45,9 @@ describe('HTML Block', () => {
<h1>Hello World</h1>
<script>mockFn()</script>
`;
const elem = <HTMLBlock html={html} runScripts={true} />;
const view = renderToString(elem);
expect(elem.props.runScripts).toBe(true);
expect(view.indexOf('<script>')).toBeLessThan(0);
expect(view.indexOf('<h1>')).toBeGreaterThanOrEqual(0);
const { container } = render(<HTMLBlock html={html} runScripts={true} />);

expect(container.querySelector('script')).not.toBeInTheDocument();
});

it('renders the html in a `<pre>` tag if safeMode={true}', () => {
Expand All @@ -62,8 +59,8 @@ ${JSON.stringify({
[/block]
`;

expect(renderToString(react(md, { safeMode: true }))).toMatchInlineSnapshot(
'"<pre class=\\"html-unsafe\\" data-reactroot=\\"\\"><code>&lt;button onload=&quot;alert(&#39;gotcha!&#39;)&quot;/&gt;</code></pre>"',
);
const { container } = render(react(md, { safeMode: true }));

expect(container.querySelector('pre > code').innerHTML).toBe('&lt;button onload="alert(\'gotcha!\')"/&gt;');
});
});
25 changes: 11 additions & 14 deletions __tests__/components/Style.test.jsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
const { renderToString } = require('react-dom/server');
const { render } = require('@testing-library/react');

const { react } = require('../../index');

describe('Style tag', () => {
describe('safeMode = false', () => {
it('renders as a style tag', () => {
const md = '<style>{ background-color: salmon }</style>';
const { container } = render(react(md));

expect(renderToString(react(md))).toMatchInlineSnapshot(
'"<style data-reactroot=\\"\\">{ background-color: salmon }</style>"',
);
expect(container.querySelector('style').innerHTML).toBe('{ background-color: salmon }');
});
});

describe('safeMode = true', () => {
it('renders the style in a `<pre>`', () => {
const md = '<style>{ background-color: salmon }</style>';
const { container } = render(react(md, { safeMode: true }));

expect(renderToString(react(md, { safeMode: true }))).toMatchInlineSnapshot(`
"<pre data-reactroot=\\"\\"><code>&lt;style&gt;
{ background-color: salmon }
&lt;/style&gt;</code></pre>"
`);
expect(container.querySelector('pre > code').innerHTML).toBe(
'&lt;style&gt;\n{ background-color: salmon }\n&lt;/style&gt;',
);
});

it('renders the style in a `<pre>` when attrs are present', () => {
const md = '<style someAttr="muahaha">{ background-color: salmon }</style>';
const { container } = render(react(md, { safeMode: true }));

expect(renderToString(react(md, { safeMode: true }))).toMatchInlineSnapshot(`
"<pre data-reactroot=\\"\\"><code>&lt;style&gt;
{ background-color: salmon }
&lt;/style&gt;</code></pre>"
`);
expect(container.querySelector('pre > code').innerHTML).toBe(
'&lt;style&gt;\n{ background-color: salmon }\n&lt;/style&gt;',
);
});
});
});
12 changes: 5 additions & 7 deletions __tests__/components/style-attributes.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
const { renderToString } = require('react-dom/server');
const { render } = require('@testing-library/react');

const { react } = require('../../index');

describe('style attributes', () => {
describe('safeMode = false', () => {
it('renders style attributes', () => {
const md = '<div style="background-color: salmon">fish content</div>';
const { container } = render(react(md));

expect(renderToString(react(md))).toMatchInlineSnapshot(
'"<div style=\\"background-color:salmon\\" data-reactroot=\\"\\">fish content</div>"',
);
expect(container.querySelector('[style]')).toBeVisible();
});
});

describe('safeMode = true', () => {
it('does not render style attributes', () => {
const md = '<div style="background-color: salmon">fish content</div>';
const { container } = render(react(md, { safeMode: true }));

expect(renderToString(react(md, { safeMode: true }))).toMatchInlineSnapshot(
'"<div data-reactroot=\\"\\">fish content</div>"',
);
expect(container.querySelector('[style]')).toBeNull();
});
});
});
22 changes: 3 additions & 19 deletions example/index.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
/* eslint-disable import/no-import-module-exports */
import React from 'react';
import ReactDOM from 'react-dom';
// eslint-disable-next-line import/no-extraneous-dependencies
import { AppContainer } from 'react-hot-loader';
import { createRoot } from 'react-dom/client';

import Demo from './Demo';

function render(Component) {
ReactDOM.render(
<AppContainer>
<Component />
</AppContainer>,
document.getElementById('rdmd-demo'),
);
}

render(Demo);

// Webpack Hot Module Replacement API
if (module.hot) {
module.hot.accept('./Demo', () => render(Demo));
}
const root = createRoot(document.getElementById('rdmd-demo'));
root.render(<Demo />);
7 changes: 7 additions & 0 deletions example/index.legacy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable import/no-import-module-exports */
import React from 'react';
import ReactDOM from 'react-dom';

import Demo from './Demo';

ReactDOM.render(<Demo />, document.getElementById('rdmd-demo'));
Loading

0 comments on commit d88f3bf

Please sign in to comment.