PagePath.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 shortPath = this.getShortPath(page.path);
  23. const pathPrefix = page.path.replace(new RegExp(shortPath + '(/)?$'), '');
  24. return (
  25. <span className="page-path">
  26. {pathPrefix}<strong>{shortPath}</strong>
  27. </span>
  28. );
  29. }
  30. }
  31. PagePath.propTypes = {
  32. page: React.PropTypes.object.isRequired,
  33. };
  34. PagePath.defaultProps = {
  35. page: {},
  36. };