PublishLink.jsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import path from 'path';
  4. export default class PublishLink extends React.PureComponent {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. linker: '',
  9. };
  10. this.generateLink = this.generateLink.bind(this);
  11. }
  12. generateLink() {
  13. const {
  14. link,
  15. label,
  16. type,
  17. isUseRelativePath,
  18. currentPagePath,
  19. } = this.props;
  20. let linker;
  21. let reshapedLink = link;
  22. if (isUseRelativePath && link.match(/^\//)) {
  23. reshapedLink = path.relative(currentPagePath, link);
  24. }
  25. if (type === 'pukiwikiLink') {
  26. linker = `[[${label}>${reshapedLink}]]`;
  27. }
  28. if (type === 'growiLink') {
  29. linker = `[${reshapedLink}]`;
  30. }
  31. if (type === 'mdLink') {
  32. linker = `[${label}](${reshapedLink})`;
  33. }
  34. this.setState({ linker });
  35. }
  36. render() {
  37. this.generateLink();
  38. return (
  39. <input type="text" readOnly className="form-control-plaintext" id="staticEmail" value={this.state.linker}></input>
  40. );
  41. }
  42. }
  43. PublishLink.propTypes = {
  44. link: PropTypes.string.isRequired,
  45. label: PropTypes.string,
  46. type: PropTypes.oneOf(['pukiwikiLink', 'growiLink', 'mdLink']).isRequired,
  47. isUseRelativePath: PropTypes.bool,
  48. currentPagePath: PropTypes.string,
  49. };