Commit 50180cb1 authored by Alexander Oleynikov's avatar Alexander Oleynikov Committed by Kushal Pandya

Refactor `readmeFile`

parent 47e0b3ee
const MARKDOWN_EXTENSIONS = ['mdown', 'mkd', 'mkdn', 'md', 'markdown']; const FILENAMES = ['index', 'readme'];
const ASCIIDOC_EXTENSIONS = ['adoc', 'ad', 'asciidoc'];
const OTHER_EXTENSIONS = ['textile', 'rdoc', 'org', 'creole', 'wiki', 'mediawiki', 'rst'];
const EXTENSIONS = [...MARKDOWN_EXTENSIONS, ...ASCIIDOC_EXTENSIONS, ...OTHER_EXTENSIONS];
const PLAIN_FILENAMES = ['readme', 'index'];
const FILE_REGEXP = new RegExp(
`^(${PLAIN_FILENAMES.join('|')})(.(${EXTENSIONS.join('|')}))?$`,
'i',
);
const PLAIN_FILE_REGEXP = new RegExp(`^(${PLAIN_FILENAMES.join('|')})`, 'i');
const EXTENSIONS_REGEXP = new RegExp(`.(${EXTENSIONS.join('|')})$`, 'i');
// eslint-disable-next-line import/prefer-default-export const MARKUP_EXTENSIONS = [
export const readmeFile = blobs => { 'ad',
const readMeFiles = blobs.filter(f => f.name.search(FILE_REGEXP) !== -1); 'adoc',
'asciidoc',
'creole',
'markdown',
'md',
'mdown',
'mediawiki',
'mkd',
'mkdn',
'org',
'rdoc',
'rst',
'textile',
'wiki',
];
const previewableReadme = readMeFiles.find(f => f.name.search(EXTENSIONS_REGEXP) !== -1); const isRichReadme = file => {
const plainReadme = readMeFiles.find(f => f.name.search(PLAIN_FILE_REGEXP) !== -1); const re = new RegExp(`^(${FILENAMES.join('|')})\\.(${MARKUP_EXTENSIONS.join('|')})$`, 'i');
return re.test(file.name);
};
return previewableReadme || plainReadme; const isPlainReadme = file => {
const re = new RegExp(`^(${FILENAMES.join('|')})$`, 'i');
return re.test(file.name);
}; };
// eslint-disable-next-line import/prefer-default-export
export const readmeFile = blobs => blobs.find(isRichReadme) || blobs.find(isPlainReadme);
import { readmeFile } from '~/repository/utils/readme'; import { readmeFile } from '~/repository/utils/readme';
describe('readmeFile', () => { describe('readmeFile', () => {
describe('markdown files', () => { it('prefers README with markup over plain text README', () => {
it('returns markdown file', () => { expect(readmeFile([{ name: 'README' }, { name: 'README.md' }])).toEqual({
expect(readmeFile([{ name: 'README' }, { name: 'README.md' }])).toEqual({ name: 'README.md',
name: 'README.md', });
}); });
expect(readmeFile([{ name: 'README' }, { name: 'index.md' }])).toEqual({ it('is case insensitive', () => {
name: 'index.md', expect(readmeFile([{ name: 'README' }, { name: 'readme.rdoc' }])).toEqual({
}); name: 'readme.rdoc',
}); });
}); });
describe('plain files', () => { it('returns the first README found', () => {
it('returns plain file', () => { expect(readmeFile([{ name: 'INDEX.adoc' }, { name: 'README.md' }])).toEqual({
expect(readmeFile([{ name: 'README' }, { name: 'TEST.md' }])).toEqual({ name: 'INDEX.adoc',
name: 'README', });
}); });
expect(readmeFile([{ name: 'readme' }, { name: 'TEST.md' }])).toEqual({ it('expects extension to be separated by dot', () => {
name: 'readme', expect(readmeFile([{ name: 'readmeXorg' }, { name: 'index.org' }])).toEqual({
}); name: 'index.org',
}); });
}); });
describe('non-previewable file', () => { it('returns plain text README when there is no README with markup', () => {
it('returns undefined', () => { expect(readmeFile([{ name: 'README' }, { name: 'NOT_README.md' }])).toEqual({
expect(readmeFile([{ name: 'index.js' }, { name: 'TEST.md' }])).toBe(undefined); name: 'README',
}); });
}); });
it('returns undefined when there are no appropriate files', () => {
expect(readmeFile([{ name: 'index.js' }, { name: 'md.README' }])).toBe(undefined);
expect(readmeFile([])).toBe(undefined);
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment