1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00

feat: adds support for ascii-doc preview in readme (#464)

This commit is contained in:
ayusharma 2018-06-20 00:15:25 +02:00
parent 825f5a57ea
commit 29bb57ad5f
6 changed files with 132 additions and 4 deletions

67
flow-typed/npm/asciidoctor.js_vx.x.x.js vendored Normal file

@ -0,0 +1,67 @@
// flow-typed signature: b7109b7e394ff03ed211118d5af4cff8
// flow-typed version: <<STUB>>/asciidoctor.js_v1.5.6/flow_v0.69.0
/**
* This is an autogenerated libdef stub for:
*
* 'asciidoctor.js'
*
* Fill this stub out by replacing all the `any` types.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/
declare module 'asciidoctor.js' {
declare module.exports: any;
}
/**
* We include stubs for each file inside this npm package in case you need to
* require those files directly. Feel free to delete any files that aren't
* needed.
*/
declare module 'asciidoctor.js/dist/asciidoctor' {
declare module.exports: any;
}
declare module 'asciidoctor.js/dist/asciidoctor.min' {
declare module.exports: any;
}
declare module 'asciidoctor.js/dist/browser/asciidoctor' {
declare module.exports: any;
}
declare module 'asciidoctor.js/dist/nashorn/asciidoctor' {
declare module.exports: any;
}
declare module 'asciidoctor.js/dist/node/asciidoctor' {
declare module.exports: any;
}
declare module 'asciidoctor.js/dist/umd/asciidoctor' {
declare module.exports: any;
}
// Filename aliases
declare module 'asciidoctor.js/dist/asciidoctor.js' {
declare module.exports: $Exports<'asciidoctor.js/dist/asciidoctor'>;
}
declare module 'asciidoctor.js/dist/asciidoctor.min.js' {
declare module.exports: $Exports<'asciidoctor.js/dist/asciidoctor.min'>;
}
declare module 'asciidoctor.js/dist/browser/asciidoctor.js' {
declare module.exports: $Exports<'asciidoctor.js/dist/browser/asciidoctor'>;
}
declare module 'asciidoctor.js/dist/nashorn/asciidoctor.js' {
declare module.exports: $Exports<'asciidoctor.js/dist/nashorn/asciidoctor'>;
}
declare module 'asciidoctor.js/dist/node/asciidoctor.js' {
declare module.exports: $Exports<'asciidoctor.js/dist/node/asciidoctor'>;
}
declare module 'asciidoctor.js/dist/umd/asciidoctor.js' {
declare module.exports: $Exports<'asciidoctor.js/dist/umd/asciidoctor'>;
}

@ -19,6 +19,7 @@
"@verdaccio/local-storage": "1.1.2",
"@verdaccio/streams": "1.0.0",
"JSONStream": "1.3.2",
"asciidoctor.js": "1.5.6",
"async": "2.6.0",
"body-parser": "1.18.2",
"bunyan": "1.8.12",

@ -1,9 +1,8 @@
// @flow
import _ from 'lodash';
import {addScope, addGravatarSupport, deleteProperties, sortByName, DIST_TAGS} from '../../../lib/utils';
import {addScope, addGravatarSupport, deleteProperties, sortByName, DIST_TAGS, parseReadme} from '../../../lib/utils';
import {allow} from '../../middleware';
import marked from 'marked';
import type {Router} from 'express';
import type {
IAuth,
@ -71,7 +70,7 @@ function addPackageWebApi(route: Router, storage: IStorageHandler, auth: IAuth)
}
res.set('Content-Type', 'text/plain');
next(marked(info.readme || 'ERROR: No README data found!'));
next(parseReadme(info.name, info.readme));
},
});
});

@ -10,6 +10,8 @@ import _ from 'lodash';
import createError from 'http-errors';
import type {Package} from '@verdaccio/types';
import type {$Request} from 'express';
import marked from 'marked';
import asciidoctor from 'asciidoctor.js';
import type {StringValue} from '../../types';
const Logger = require('./logger');
@ -437,6 +439,31 @@ function addGravatarSupport(pkgInfo: any) {
return pkgInfo;
}
/**
* parse package readme - markdown/ascii
* @param {String} packageName name of package
* @param {String} readme package readme
* @return {String} converted html template
*/
function parseReadme(packageName: string, readme: string): string {
const ascii = asciidoctor();
const docTypeIdentifier = new RegExp(/^=+ \w/, 'g');
// asciidoc
if (docTypeIdentifier.test(readme)) {
return ascii.convert(readme, {safe: 'safe', attributes: {showtitle: true, icons: 'font'}});
}
if (readme) {
return marked(readme);
}
// logs readme not found error
Logger.logger.error({packageName}, '@{packageName}: No readme found');
return marked('ERROR: No README data found!');
}
export {
addGravatarSupport,
deleteProperties,
@ -458,4 +485,5 @@ export {
getLatestVersion,
ErrorCode,
parseConfigFile,
parseReadme,
};

@ -1,9 +1,12 @@
// @flow
import assert from 'assert';
import {validateName as validate, convertDistRemoteToLocalTarballUrls} from '../../src/lib/utils';
import {validateName as validate, convertDistRemoteToLocalTarballUrls, parseReadme} from '../../src/lib/utils';
import {generateGravatarUrl, GRAVATAR_DEFAULT} from '../../src/utils/user';
import {spliceURL} from '../../src/utils/string';
import Package from "../../src/webui/src/components/Package";
import Logger, {setup} from '../../src/lib/logger';
setup([]);
describe('Utilities', () => {
@ -110,4 +113,34 @@ describe('Utilities', () => {
expect(convertDist.versions['1.0.1'].dist.tarball).toEqual(buildURI(host, '1.0.1'));
});
});
describe('parseReadme', () => {
test('should pass for ascii/makrdown text to html template', () => {
const markdown = '# markdown';
const ascii = "= AsciiDoc";
expect(parseReadme('testPackage', markdown)).toEqual('<h1 id="markdown">markdown</h1>\n');
expect(parseReadme('testPackage', ascii)).toEqual('<h1>AsciiDoc</h1>\n');
});
test('should pass for conversion of non-ascii to markdown text', () => {
const simpleText = 'simple text';
const randomText = '%%%%%**##==';
const randomTextNonAscii = 'simple text \n = ascii';
const randomTextMarkdown = 'simple text \n # markdown';
expect(parseReadme('testPackage', randomText)).toEqual('<p>%%%%%**##==</p>\n');
expect(parseReadme('testPackage', simpleText)).toEqual('<p>simple text</p>\n');
expect(parseReadme('testPackage', randomTextNonAscii))
.toEqual('<p>simple text \n = ascii</p>\n');
expect(parseReadme('testPackage', randomTextMarkdown))
.toEqual('<p>simple text </p>\n<h1 id="markdown">markdown</h1>\n');
});
test('should show error for no readme data', () => {
const noData = '';
const spy = jest.spyOn(Logger.logger, 'error')
expect(parseReadme('testPackage', noData))
.toEqual('<p>ERROR: No README data found!</p>\n');
expect(spy).toHaveBeenCalledWith({'packageName': 'testPackage'}, '@{packageName}: No readme found');
});
});
});

BIN
yarn.lock

Binary file not shown.