feat: highlight readme source code (#3506)

* feat: highlight readme source code

* chore: fix tests

* Delete utils.spec.ts.snap
This commit is contained in:
Juan Picado 2022-11-19 22:00:56 +01:00 committed by GitHub
parent 07144c9886
commit 8715a5cfa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 32 additions and 142 deletions

49
.pnp.cjs generated
View File

@ -78,10 +78,9 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
["@verdaccio-scope/verdaccio-auth-foo", "npm:0.0.2"],\
["@verdaccio/commons-api", "npm:10.2.0"],\
["@verdaccio/local-storage", "npm:10.3.1"],\
["@verdaccio/readme", "npm:10.4.2"],\
["@verdaccio/streams", "npm:10.2.0"],\
["@verdaccio/types", "npm:10.7.0"],\
["@verdaccio/ui-theme", "npm:6.0.0-6-next.50"],\
["@verdaccio/ui-theme", "npm:6.0.0-6-next.51"],\
["JSONStream", "npm:1.3.5"],\
["all-contributors-cli", "npm:6.20.0"],\
["async", "npm:3.2.4"],\
@ -4845,18 +4844,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
"linkType": "HARD"\
}]\
]],\
["@verdaccio/readme", [\
["npm:10.4.2", {\
"packageLocation": "./.yarn/cache/@verdaccio-readme-npm-10.4.2-2cca0c072d-73492f1137.zip/node_modules/@verdaccio/readme/",\
"packageDependencies": [\
["@verdaccio/readme", "npm:10.4.2"],\
["dompurify", "npm:2.4.0"],\
["jsdom", "virtual:2cca0c072d54eb622535386134c131ff0320958b9ce0515b5a1e0aef7ea16a35e036ec0ef2d2625d264420121ebbd88b85bd24e2b5bdc2d4f075f9beaf958114#npm:16.7.0"],\
["marked", "npm:4.2.2"]\
],\
"linkType": "HARD"\
}]\
]],\
["@verdaccio/streams", [\
["npm:10.2.0", {\
"packageLocation": "./.yarn/cache/@verdaccio-streams-npm-10.2.0-81db112291-2c5fd4953f.zip/node_modules/@verdaccio/streams/",\
@ -4876,10 +4863,10 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
}]\
]],\
["@verdaccio/ui-theme", [\
["npm:6.0.0-6-next.50", {\
"packageLocation": "./.yarn/cache/@verdaccio-ui-theme-npm-6.0.0-6-next.50-635b2acf02-1e7ac1608f.zip/node_modules/@verdaccio/ui-theme/",\
["npm:6.0.0-6-next.51", {\
"packageLocation": "./.yarn/cache/@verdaccio-ui-theme-npm-6.0.0-6-next.51-7dbd590283-b478cb7cd8.zip/node_modules/@verdaccio/ui-theme/",\
"packageDependencies": [\
["@verdaccio/ui-theme", "npm:6.0.0-6-next.50"]\
["@verdaccio/ui-theme", "npm:6.0.0-6-next.51"]\
],\
"linkType": "HARD"\
}]\
@ -7296,15 +7283,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
"linkType": "HARD"\
}]\
]],\
["dompurify", [\
["npm:2.4.0", {\
"packageLocation": "./.yarn/cache/dompurify-npm-2.4.0-0ffecf22ef-c93ea73cf8.zip/node_modules/dompurify/",\
"packageDependencies": [\
["dompurify", "npm:2.4.0"]\
],\
"linkType": "HARD"\
}]\
]],\
["dot-prop", [\
["npm:5.3.0", {\
"packageLocation": "./.yarn/cache/dot-prop-npm-5.3.0-7bf6ee1eb8-d577579009.zip/node_modules/dot-prop/",\
@ -10297,7 +10275,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
["@types/node", "npm:18.11.9"],\
["jest-mock", "npm:26.6.2"],\
["jest-util", "npm:26.6.2"],\
["jsdom", "virtual:2cca0c072d54eb622535386134c131ff0320958b9ce0515b5a1e0aef7ea16a35e036ec0ef2d2625d264420121ebbd88b85bd24e2b5bdc2d4f075f9beaf958114#npm:16.7.0"]\
["jsdom", "virtual:defa486869c88441047200a53b3aa18d79743b272095f3ee31b5b7b80b2c93d87f722added867470dcb94104504489a1a89040ea8fd89dffb9cfb1864d4bf54e#npm:16.7.0"]\
],\
"linkType": "HARD"\
}]\
@ -10706,10 +10684,10 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
],\
"linkType": "SOFT"\
}],\
["virtual:2cca0c072d54eb622535386134c131ff0320958b9ce0515b5a1e0aef7ea16a35e036ec0ef2d2625d264420121ebbd88b85bd24e2b5bdc2d4f075f9beaf958114#npm:16.7.0", {\
"packageLocation": "./.yarn/__virtual__/jsdom-virtual-518970426e/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/",\
["virtual:defa486869c88441047200a53b3aa18d79743b272095f3ee31b5b7b80b2c93d87f722added867470dcb94104504489a1a89040ea8fd89dffb9cfb1864d4bf54e#npm:16.7.0", {\
"packageLocation": "./.yarn/__virtual__/jsdom-virtual-d1d747a211/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/",\
"packageDependencies": [\
["jsdom", "virtual:2cca0c072d54eb622535386134c131ff0320958b9ce0515b5a1e0aef7ea16a35e036ec0ef2d2625d264420121ebbd88b85bd24e2b5bdc2d4f075f9beaf958114#npm:16.7.0"],\
["jsdom", "virtual:defa486869c88441047200a53b3aa18d79743b272095f3ee31b5b7b80b2c93d87f722added867470dcb94104504489a1a89040ea8fd89dffb9cfb1864d4bf54e#npm:16.7.0"],\
["@types/canvas", null],\
["abab", "npm:2.0.6"],\
["acorn", "npm:8.8.1"],\
@ -10737,7 +10715,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
["whatwg-encoding", "npm:1.0.5"],\
["whatwg-mimetype", "npm:2.3.0"],\
["whatwg-url", "npm:8.7.0"],\
["ws", "virtual:518970426e110e331af0e7205d87d1c860793eb156872ced507847acefb38dfa48c3ea74c9df56d29d4234e2bd5eca7283af0febbf0cbaac05abb89f24498f34#npm:7.5.9"],\
["ws", "virtual:d1d747a211d62a20e750f460de660a2cab0b8c26eed5e86aa53c81e5d6991bfbf78edfb74d516e5d88a3b849de28b88871ffac8dc80d6c59707fedfeaf750a82#npm:7.5.9"],\
["xml-name-validator", "npm:3.0.0"]\
],\
"packagePeers": [\
@ -15294,10 +15272,9 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
["@verdaccio-scope/verdaccio-auth-foo", "npm:0.0.2"],\
["@verdaccio/commons-api", "npm:10.2.0"],\
["@verdaccio/local-storage", "npm:10.3.1"],\
["@verdaccio/readme", "npm:10.4.2"],\
["@verdaccio/streams", "npm:10.2.0"],\
["@verdaccio/types", "npm:10.7.0"],\
["@verdaccio/ui-theme", "npm:6.0.0-6-next.50"],\
["@verdaccio/ui-theme", "npm:6.0.0-6-next.51"],\
["JSONStream", "npm:1.3.5"],\
["all-contributors-cli", "npm:6.20.0"],\
["async", "npm:3.2.4"],\
@ -15650,10 +15627,10 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
],\
"linkType": "SOFT"\
}],\
["virtual:518970426e110e331af0e7205d87d1c860793eb156872ced507847acefb38dfa48c3ea74c9df56d29d4234e2bd5eca7283af0febbf0cbaac05abb89f24498f34#npm:7.5.9", {\
"packageLocation": "./.yarn/__virtual__/ws-virtual-9ffc40a239/0/cache/ws-npm-7.5.9-26f12a5ed6-c3c100a181.zip/node_modules/ws/",\
["virtual:d1d747a211d62a20e750f460de660a2cab0b8c26eed5e86aa53c81e5d6991bfbf78edfb74d516e5d88a3b849de28b88871ffac8dc80d6c59707fedfeaf750a82#npm:7.5.9", {\
"packageLocation": "./.yarn/__virtual__/ws-virtual-1d5cf10b03/0/cache/ws-npm-7.5.9-26f12a5ed6-c3c100a181.zip/node_modules/ws/",\
"packageDependencies": [\
["ws", "virtual:518970426e110e331af0e7205d87d1c860793eb156872ced507847acefb38dfa48c3ea74c9df56d29d4234e2bd5eca7283af0febbf0cbaac05abb89f24498f34#npm:7.5.9"],\
["ws", "virtual:d1d747a211d62a20e750f460de660a2cab0b8c26eed5e86aa53c81e5d6991bfbf78edfb74d516e5d88a3b849de28b88871ffac8dc80d6c59707fedfeaf750a82#npm:7.5.9"],\
["@types/bufferutil", null],\
["@types/utf-8-validate", null],\
["bufferutil", null],\

View File

@ -6,11 +6,12 @@ ENV NODE_ENV=production \
CI=true \
HUSKY_DEBUG=1
RUN apk --no-cache add openssl ca-certificates wget && \
RUN apk add --force-overwrite && \
apk --no-cache add openssl ca-certificates wget && \
apk --no-cache add g++ gcc libgcc libstdc++ linux-headers make python3 && \
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
wget -q https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk && \
apk add glibc-2.29-r0.apk
wget -q https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-2.35-r0.apk && \
apk add --force-overwrite glibc-2.35-r0.apk
WORKDIR /opt/verdaccio-build
COPY . .

View File

@ -20,9 +20,8 @@
"dependencies": {
"@verdaccio/commons-api": "10.2.0",
"@verdaccio/local-storage": "10.3.1",
"@verdaccio/readme": "10.4.2",
"@verdaccio/streams": "10.2.0",
"@verdaccio/ui-theme": "6.0.0-6-next.50",
"@verdaccio/ui-theme": "6.0.0-6-next.51",
"JSONStream": "1.3.5",
"async": "3.2.4",
"body-parser": "1.20.1",

View File

@ -105,7 +105,7 @@ function addPackageWebApi(storage: IStorageHandler, auth: IAuth, config: Config)
res.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.TEXT_PLAIN);
const referer = req.get('Referer');
const pathname = referer ? new URL(referer).pathname : undefined;
next(parseReadme(info.name, info.readme, { pathname }));
next(parseReadme(info.name, info.readme));
},
});
});

View File

@ -11,7 +11,6 @@ import validator from 'validator';
// eslint-disable-next-line max-len
import { getBadData, getBadRequest, getCode, getConflict, getForbidden, getInternalError, getNotFound, getServiceUnavailable, getUnauthorized } from '@verdaccio/commons-api';
import sanitizyReadme from '@verdaccio/readme';
import { Author, Config, Package, Version } from '@verdaccio/types';
import { AuthorAvatar, StringValue } from '../../types';
@ -494,18 +493,18 @@ export function addGravatarSupport(pkgInfo: Package, online = true): AuthorAvata
* parse package readme - markdown/ascii
* @param {String} packageName name of package
* @param {String} readme package readme
* @param {Object} options sanitizyReadme options
* @return {String} converted html template
*/
export function parseReadme(packageName: string, readme: string, options: { pathname?: string | void } = {}): string | void {
export function parseReadme(packageName: string, readme: string): string | void {
if (_.isEmpty(readme) === false) {
return sanitizyReadme(readme, options);
return readme;
}
// logs readme not found error
logger.error({ packageName }, '@{packageName}: No readme found');
logger.info({ packageName }, '@{packageName}: No readme found');
return sanitizyReadme('ERROR: No README data found!');
return 'ERROR: No README data found!';
}
export function buildToken(type: string, token: string): string {

View File

@ -1,51 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Utilities parseReadme should parse 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>"
`;

View File

@ -404,26 +404,10 @@ describe('Utilities', () => {
});
describe('parseReadme', () => {
test('should parse makrdown text to html template', () => {
const markdown = '# markdown';
expect(parseReadme('testPackage', markdown)).toEqual('<h1 id="markdown">markdown</h1>');
expect(parseReadme('testPackage', String(readmeFile('markdown.md')))).toMatchSnapshot();
});
test('should pass for conversion of non-ascii to markdown text', () => {
const simpleText = 'simple text';
const randomText = '%%%%%**##==';
const randomTextMarkdown = 'simple text \n # markdown';
expect(parseReadme('testPackage', randomText)).toEqual('<p>%%%%%**##==</p>');
expect(parseReadme('testPackage', simpleText)).toEqual('<p>simple text</p>');
expect(parseReadme('testPackage', randomTextMarkdown)).toEqual('<p>simple text </p>\n<h1 id="markdown">markdown</h1>');
});
test('should show error for no readme data', () => {
const noData = '';
const spy = jest.spyOn(logger, 'error');
expect(parseReadme('testPackage', noData)).toEqual('<p>ERROR: No README data found!</p>');
const spy = jest.spyOn(logger, 'info');
expect(parseReadme('testPackage', noData)).toEqual('ERROR: No README data found!');
expect(spy).toHaveBeenCalledWith({ packageName: 'testPackage' }, '@{packageName}: No readme found');
});
});

View File

@ -2978,17 +2978,6 @@ __metadata:
languageName: node
linkType: hard
"@verdaccio/readme@npm:10.4.2":
version: 10.4.2
resolution: "@verdaccio/readme@npm:10.4.2"
dependencies:
dompurify: 2.4.0
jsdom: 16.7.0
marked: 4.2.2
checksum: 73492f11378cb2d4047caa693bde9e322b730b1d79c84ecea92098b4fda104b644d6098b016819069c5a7482bff993e28c35fbbc69f98f23ce0a042cc30dbfc4
languageName: node
linkType: hard
"@verdaccio/streams@npm:10.2.0":
version: 10.2.0
resolution: "@verdaccio/streams@npm:10.2.0"
@ -3003,10 +2992,10 @@ __metadata:
languageName: node
linkType: hard
"@verdaccio/ui-theme@npm:6.0.0-6-next.50":
version: 6.0.0-6-next.50
resolution: "@verdaccio/ui-theme@npm:6.0.0-6-next.50"
checksum: 1e7ac1608f89cacb058c6b822928ef5828d8797419a5bf8470502f17d4b3af9e48037ed22d19d5efa8d12612131d4fc4eef4bf7aa1b281b5b685cafc30b5583c
"@verdaccio/ui-theme@npm:6.0.0-6-next.51":
version: 6.0.0-6-next.51
resolution: "@verdaccio/ui-theme@npm:6.0.0-6-next.51"
checksum: b478cb7cd8bef2b6dfe924728d9869765b5f540536df84dfdb2318406c595cef39ab80d5f4a2296795ad476ea427f477830212170f9c10cd01f74dc728fe0481
languageName: node
linkType: hard
@ -4994,13 +4983,6 @@ __metadata:
languageName: node
linkType: hard
"dompurify@npm:2.4.0":
version: 2.4.0
resolution: "dompurify@npm:2.4.0"
checksum: c93ea73cf8e3ba044588450198563e56ce6902e36d0e16e3699df2fa59e82c4fdd11d4ad04ef5024569ce96a35b46f29d0bbea522516add33cd39a7f56a8a675
languageName: node
linkType: hard
"dot-prop@npm:^5.1.0":
version: 5.3.0
resolution: "dot-prop@npm:5.3.0"
@ -7950,7 +7932,7 @@ __metadata:
languageName: node
linkType: hard
"jsdom@npm:16.7.0, jsdom@npm:^16.4.0":
"jsdom@npm:^16.4.0":
version: 16.7.0
resolution: "jsdom@npm:16.7.0"
dependencies:
@ -12115,10 +12097,9 @@ __metadata:
"@verdaccio-scope/verdaccio-auth-foo": 0.0.2
"@verdaccio/commons-api": 10.2.0
"@verdaccio/local-storage": 10.3.1
"@verdaccio/readme": 10.4.2
"@verdaccio/streams": 10.2.0
"@verdaccio/types": 10.7.0
"@verdaccio/ui-theme": 6.0.0-6-next.50
"@verdaccio/ui-theme": 6.0.0-6-next.51
JSONStream: 1.3.5
all-contributors-cli: 6.20.0
async: 3.2.4