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": "^0.93.0",
"three-full": "^17.1.0", "three-full": "^17.1.0",
"tree-kill": "^1.1.0", "tree-kill": "^1.1.0",
"unist-util-visit": "^1.4.1",
"video.js": "^7.2.2", "video.js": "^7.2.2",
"villain": "btzr-io/Villain", "villain": "btzr-io/Villain",
"wavesurfer.js": "^2.2.1", "wavesurfer.js": "^2.2.1",

View file

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

View file

@ -1,4 +1,5 @@
import { parseURI } from 'lbry-redux'; import { parseURI } from 'lbry-redux';
import visit from 'unist-util-visit';
const protocol = 'lbry://'; const protocol = 'lbry://';
const locateURI = (value, fromIndex) => value.indexOf(protocol, fromIndex); const locateURI = (value, fromIndex) => value.indexOf(protocol, fromIndex);
@ -46,16 +47,44 @@ function tokenizeURI(eat, value, silent) {
// Configure tokenizer for lbry urls // Configure tokenizer for lbry urls
tokenizeURI.locator = locateURI; tokenizeURI.locator = locateURI;
tokenizeURI.notInList = true;
tokenizeURI.notInLink = true; tokenizeURI.notInLink = true;
tokenizeURI.notInBlock = true; tokenizeURI.notInBlock = true;
// Configure tokenizer for lbry channels // Configure tokenizer for lbry channels
tokenizeMention.locator = locateMention; tokenizeMention.locator = locateMention;
tokenizeMention.notInList = true;
tokenizeMention.notInLink = true; tokenizeMention.notInLink = true;
tokenizeMention.notInBlock = 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 // Main module
export default function remarkLBRY() { export function inlineLinks() {
const Parser = this.Parser; const Parser = this.Parser;
const tokenizers = Parser.prototype.inlineTokenizers; const tokenizers = Parser.prototype.inlineTokenizers;
const methods = Parser.prototype.inlineMethods; const methods = Parser.prototype.inlineMethods;

View file

@ -11528,7 +11528,7 @@ unist-util-visit-parents@^2.0.0:
dependencies: dependencies:
unist-util-is "^2.1.2" 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" version "1.4.1"
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-1.4.1.tgz#4724aaa8486e6ee6e26d7ff3c8685960d560b1e3" resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-1.4.1.tgz#4724aaa8486e6ee6e26d7ff3c8685960d560b1e3"
integrity sha512-AvGNk7Bb//EmJZyhtRUnNMEpId/AZ5Ph/KUpTI09WHQuDZHKovQ1oEv3mfmKpWKtoMzyMC4GLBm1Zy5k12fjIw== integrity sha512-AvGNk7Bb//EmJZyhtRUnNMEpId/AZ5Ph/KUpTI09WHQuDZHKovQ1oEv3mfmKpWKtoMzyMC4GLBm1Zy5k12fjIw==