detect formated links

This commit is contained in:
btzr-io 2019-06-10 00:20:40 -06:00
parent ad3689c84d
commit b6ed7c12cc
4 changed files with 38 additions and 4 deletions

View file

@ -175,6 +175,7 @@
"three": "^0.93.0",
"three-full": "^17.1.0",
"tree-kill": "^1.1.0",
"unist-util-visit": "^1.4.1",
"video.js": "^7.2.2",
"villain": "btzr-io/Villain",
"wavesurfer.js": "^2.2.1",

View file

@ -1,12 +1,12 @@
// @flow
import * as React from 'react';
import remark from 'remark';
import remarkLBRY from 'util/remark-lbry';
import remarkStrip from 'strip-markdown';
import remarkEmoji from 'remark-emoji';
import reactRenderer from 'remark-react';
import ExternalLink from 'component/externalLink';
import defaultSchema from 'hast-util-sanitize/lib/github.json';
import { formatedLinks, inlineLinks } from 'util/remark-lbry';
type SimpleTextProps = {
children?: React.Node,
@ -77,7 +77,11 @@ const MarkdownPreview = (props: MarkdownProps) => {
<div className="markdown-preview">
{
remark()
.use(remarkLBRY)
// Remark plugins for lbry urls
// Note: The order is important
.use(formatedLinks)
.use(inlineLinks)
// Emojis
.use(remarkEmoji)
.use(reactRenderer, remarkOptions)
.processSync(content).contents

View file

@ -1,4 +1,5 @@
import { parseURI } from 'lbry-redux';
import visit from 'unist-util-visit';
const protocol = 'lbry://';
const locateURI = (value, fromIndex) => value.indexOf(protocol, fromIndex);
@ -46,16 +47,44 @@ function tokenizeURI(eat, value, silent) {
// Configure tokenizer for lbry urls
tokenizeURI.locator = locateURI;
tokenizeURI.notInList = true;
tokenizeURI.notInLink = true;
tokenizeURI.notInBlock = true;
// Configure tokenizer for lbry channels
tokenizeMention.locator = locateMention;
tokenizeMention.notInList = true;
tokenizeMention.notInLink = true;
tokenizeMention.notInBlock = true;
const visitor = (node, index, parent) => {
if (node.type === 'link') {
try {
const url = parseURI(node.url);
// Handle lbry link
if (!url.isChannel || (url.isChannel && url.path)) {
// Auto-embed lbry url
if (parent && parent.type === 'paragraph' && !node.data) {
node.data = {
hProperties: { 'data-preview': true },
};
}
}
} catch (err) {
// Silent errors: console.error(err)
}
}
};
// transform
const transform = tree => {
visit(tree, ['link'], visitor);
};
export const formatedLinks = () => transform;
// Main module
export default function remarkLBRY() {
export function inlineLinks() {
const Parser = this.Parser;
const tokenizers = Parser.prototype.inlineTokenizers;
const methods = Parser.prototype.inlineMethods;

View file

@ -11528,7 +11528,7 @@ unist-util-visit-parents@^2.0.0:
dependencies:
unist-util-is "^2.1.2"
unist-util-visit@^1.0.0, unist-util-visit@^1.1.0, unist-util-visit@^1.3.0, unist-util-visit@^1.4.0:
unist-util-visit@^1.0.0, unist-util-visit@^1.1.0, unist-util-visit@^1.3.0, unist-util-visit@^1.4.0, unist-util-visit@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-1.4.1.tgz#4724aaa8486e6ee6e26d7ff3c8685960d560b1e3"
integrity sha512-AvGNk7Bb//EmJZyhtRUnNMEpId/AZ5Ph/KUpTI09WHQuDZHKovQ1oEv3mfmKpWKtoMzyMC4GLBm1Zy5k12fjIw==