Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
50180cb1
Commit
50180cb1
authored
Jan 10, 2020
by
Alexander Oleynikov
Committed by
Kushal Pandya
Jan 10, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor `readmeFile`
parent
47e0b3ee
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
36 deletions
+52
-36
app/assets/javascripts/repository/utils/readme.js
app/assets/javascripts/repository/utils/readme.js
+28
-17
spec/frontend/repository/utils/readme_spec.js
spec/frontend/repository/utils/readme_spec.js
+24
-19
No files found.
app/assets/javascripts/repository/utils/readme.js
View file @
50180cb1
const
MARKDOWN_EXTENSIONS
=
[
'
mdown
'
,
'
mkd
'
,
'
mkdn
'
,
'
md
'
,
'
markdown
'
];
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
'
);
const
FILENAMES
=
[
'
index
'
,
'
readme
'
];
// eslint-disable-next-line import/prefer-default-export
export
const
readmeFile
=
blobs
=>
{
const
readMeFiles
=
blobs
.
filter
(
f
=>
f
.
name
.
search
(
FILE_REGEXP
)
!==
-
1
);
const
MARKUP_EXTENSIONS
=
[
'
ad
'
,
'
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
plainReadme
=
readMeFiles
.
find
(
f
=>
f
.
name
.
search
(
PLAIN_FILE_REGEXP
)
!==
-
1
);
const
isRichReadme
=
file
=>
{
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
);
spec/frontend/repository/utils/readme_spec.js
View file @
50180cb1
import
{
readmeFile
}
from
'
~/repository/utils/readme
'
;
describe
(
'
readmeFile
'
,
()
=>
{
describe
(
'
markdown files
'
,
()
=>
{
it
(
'
returns markdown file
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
README
'
},
{
name
:
'
README.md
'
}])).
toEqual
({
name
:
'
README.md
'
,
});
it
(
'
prefers README with markup over plain text README
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
README
'
},
{
name
:
'
README.md
'
}])).
toEqual
(
{
name
:
'
README.md
'
,
});
});
expect
(
readmeFile
([{
name
:
'
README
'
},
{
name
:
'
index.md
'
}])).
toEqual
(
{
name
:
'
index.md
'
,
});
it
(
'
is case insensitive
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
README
'
},
{
name
:
'
readme.rdoc
'
}])).
toEqual
({
name
:
'
readme.rdoc
'
,
});
});
describe
(
'
plain files
'
,
()
=>
{
it
(
'
returns plain file
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
README
'
},
{
name
:
'
TEST.md
'
}])).
toEqual
({
name
:
'
README
'
,
});
it
(
'
returns the first README found
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
INDEX.adoc
'
},
{
name
:
'
README.md
'
}])).
toEqual
(
{
name
:
'
INDEX.adoc
'
,
});
});
expect
(
readmeFile
([{
name
:
'
readme
'
},
{
name
:
'
TEST.md
'
}])).
toEqual
(
{
name
:
'
readme
'
,
});
it
(
'
expects extension to be separated by dot
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
readmeXorg
'
},
{
name
:
'
index.org
'
}])).
toEqual
({
name
:
'
index.org
'
,
});
});
describe
(
'
non-previewable file
'
,
()
=>
{
it
(
'
returns undefined
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
index.js
'
},
{
name
:
'
TEST.md
'
}])).
toBe
(
undefined
);
it
(
'
returns plain text README when there is no README with markup
'
,
()
=>
{
expect
(
readmeFile
([{
name
:
'
README
'
},
{
name
:
'
NOT_README.md
'
}])).
toEqual
(
{
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
);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment