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
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
);
spec/frontend/repository/utils/readme_spec.js
View file @
50180cb1
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
);
});
});
});
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