PagePath.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import React from 'react';
  2. export default class PagePath extends React.Component {
  3. getShortPath(path) {
  4. let name = path.replace(/(\/)$/, '');
  5. // /.../hoge/YYYY/MM/DD 形式のページ
  6. if (name.match(/.+\/([^/]+\/\d{4}\/\d{2}\/\d{2})$/)) {
  7. return name.replace(/.+\/([^/]+\/\d{4}\/\d{2}\/\d{2})$/, '$1');
  8. }
  9. // /.../hoge/YYYY/MM 形式のページ
  10. if (name.match(/.+\/([^/]+\/\d{4}\/\d{2})$/)) {
  11. return name.replace(/.+\/([^/]+\/\d{4}\/\d{2})$/, '$1');
  12. }
  13. // /.../hoge/YYYY 形式のページ
  14. if (name.match(/.+\/([^/]+\/\d{4})$/)) {
  15. return name.replace(/.+\/([^/]+\/\d{4})$/, '$1');
  16. }
  17. // ページの末尾を拾う
  18. return name.replace(/.+\/(.+)?$/, '$1');
  19. }
  20. render() {
  21. const page = this.props.page;
  22. const pagePath = page.path.replace(this.props.excludePathString.replace(/^\//, ''), '');
  23. const shortPath = this.getShortPath(pagePath);
  24. const shortPathEscaped = shortPath.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  25. const pathPrefix = pagePath.replace(new RegExp(shortPathEscaped + '(/)?$'), '');
  26. return (
  27. <span className="page-path">
  28. {pathPrefix}<strong>{shortPath}</strong>
  29. </span>
  30. );
  31. }
  32. }
  33. PagePath.propTypes = {
  34. page: React.PropTypes.object.isRequired,
  35. };
  36. PagePath.defaultProps = {
  37. page: {},
  38. excludePathString: '',
  39. };