MarkdownLinkUtil.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import Linker from '../../models/Linker';
  2. /**
  3. * Utility for markdown link
  4. */
  5. class MarkdownLinkUtil {
  6. constructor() {
  7. this.getMarkdownLink = this.getMarkdownLink.bind(this);
  8. this.isInLink = this.isInLink.bind(this);
  9. this.replaceFocusedMarkdownLinkWithEditor = this.replaceFocusedMarkdownLinkWithEditor.bind(this);
  10. }
  11. // return an instance of Linker from cursor position or selected text.
  12. getMarkdownLink(editor) {
  13. if (!this.isInLink(editor)) {
  14. return Linker.fromMarkdownString(editor.getDoc().getSelection());
  15. }
  16. const curPos = editor.getCursor();
  17. return Linker.fromLineWithIndex(editor.getDoc().getLine(curPos.line), curPos.ch)
  18. }
  19. isInLink(editor) {
  20. const curPos = editor.getCursor();
  21. const { beginningOfLink, endOfLink } = Linker.getBeginningAndEndIndexOfLink(editor.getDoc().getLine(curPos.line), curPos.ch);
  22. return beginningOfLink >= 0 && endOfLink >= 0;
  23. }
  24. replaceFocusedMarkdownLinkWithEditor(editor) {
  25. // GW-3023
  26. }
  27. }
  28. // singleton pattern
  29. const instance = new MarkdownLinkUtil();
  30. Object.freeze(instance);
  31. export default instance;