This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/shared/utils/parseDocumentIds.js
Tom Moor d5b5d4fc27 feat: Document hover cards (#1346)
* stash

* refactor

* refactor, styling

* tweaks

* pointer

* styling

* fi: Hide when printing

* fix: No hover cards on shared links

* remove suppressions no longer needed

* fix: Don't show hover cards when editing, they get in the way

* fix: Prevent hover card from going off rhs edge of screen

* fix: Remount hover card when changing between links

* fix: allow one part domains in links (#1350)

* allow one part domains in links

* no TLD when only one part domain

* return null for parseDomain of empty string

* fix fiddly hover preview behavior

* WIP

* refactor hover preview

* fix: Non-rounded bottom corners

* fix: Fixes an edgecase where mounting the nested editor in hovercard causesdocument to scroll if there is a hash in the url

* fix: Incorrect document preview rendering

* lint

Co-authored-by: Nan Yu <thenanyu@gmail.com>
Co-authored-by: Nan Yu <nan@getoutline.com>
2020-07-16 21:26:23 -07:00

51 lines
1.2 KiB
JavaScript

// @flow
import { parser } from "rich-markdown-editor";
export default function parseDocumentIds(text: string): string[] {
const value = parser.parse(text);
let links = [];
function findLinks(node) {
// get text nodes
if (node.type.name === "text") {
// get marks for text nodes
node.marks.forEach(mark => {
// any of the marks links?
if (mark.type.name === "link") {
const { href } = mark.attrs;
// any of the links to other docs?
if (href.startsWith("/doc")) {
const tokens = href.replace(/\/$/, "").split("/");
const lastToken = tokens[tokens.length - 1];
// don't return the same link more than once
if (!links.includes(lastToken)) {
links.push(lastToken);
}
}
}
});
}
if (!node.content.size) {
return;
}
node.content.descendants(findLinks);
}
findLinks(value);
return links;
}
export function parseDocumentSlugFromUrl(url: string) {
let parsed;
try {
parsed = new URL(url);
} catch (err) {
return;
}
return parsed.pathname.replace(/^\/doc\//, "");
}