PagePath.js 1.4 KB

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