From 07b59a67615b703f3ce03fe54f5e1ef9af4c37e0 Mon Sep 17 00:00:00 2001 From: Tasos Maroudas Date: Thu, 10 Jan 2019 16:49:36 +0200 Subject: [PATCH 1/4] Add support for bold links --- index.js | 32 +++++++++++++++++++++++++++++++- styles.js | 5 +++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b933978..cef402b 100644 --- a/index.js +++ b/index.js @@ -176,6 +176,25 @@ class Markdown extends Component { ); } + renderStrongLink(node, key) { + const { styles } = this.state; + let extras = Utils.concatStyles(null, styles.strongLink); + + // remove the empty spaces (" ") which is the result of the bold markdown charactes from node's children + for (const i = 0; i < node.props.children.length; i++) { + if (node.props.children[i] === " ") { + delete node.props.children[i]; + } + } + let children = this.renderNodes(node.props.children, key, extras); + + return ( + Linking.openURL(node.props.href).catch(() => { })}> + {children} + + ); + } + renderBlockQuote(node, key, extras) { extras = extras ? Object.assign(extras, { blockQuote: true }) : { blockQuote: true }; return this.renderBlock(node, key, extras); @@ -218,6 +237,16 @@ class Markdown extends Component { return null; } + // check when a strong node hides a link inside + if (node.type === 'strong' && typeof(node.props.children) === 'object' && node.props.children.length > 0) { + for (const i = 0; i < node.props.children.length; i++) { + // check for the link + if (children[i].type && children[i].type === 'a') { + node.type = 'stronga'; + } + } + } + const { styles } = this.state; @@ -236,7 +265,8 @@ class Markdown extends Component { case 'a': return this.renderLink(node, key); case 'img': return this.renderImage(node, key); case 'strong': return this.renderText(node, key, Utils.concatStyles(extras, styles.strong)); - case 'del': return this.renderText(node, key, Utils.concatStyles(extras, styles.del)); + case 'stronga': return this.renderStrongLink(node, key, Utils.concatStyles(extras, styles.strong)); + case 'del': return this.renderText(this.renderLink(node, key), key, Utils.concatStyles(extras, styles.del)); case 'em': return this.renderText(node, key, Utils.concatStyles(extras, styles.em)); case 'u': return this.renderText(node, key, Utils.concatStyles(extras, styles.u)); case 'blockquote': return this.renderBlockQuote(node, key); diff --git a/styles.js b/styles.js index 6b841af..c642c2e 100644 --- a/styles.js +++ b/styles.js @@ -52,6 +52,11 @@ const defaultStyles = { strong: { fontWeight: 'bold', }, + strongLink: { + fontWeight: 'bold', + textDecorationLine: 'underline', + alignSelf: 'flex-start' + }, em: { fontStyle: 'italic', }, From ea4915a29297b39d3ed14d11ac6cb55769971d55 Mon Sep 17 00:00:00 2001 From: Tasos Maroudas Date: Thu, 10 Jan 2019 17:25:48 +0200 Subject: [PATCH 2/4] Add support for bold links (#2) * Add support for bold links * Fix children reference --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index cef402b..f93fb85 100644 --- a/index.js +++ b/index.js @@ -241,7 +241,7 @@ class Markdown extends Component { if (node.type === 'strong' && typeof(node.props.children) === 'object' && node.props.children.length > 0) { for (const i = 0; i < node.props.children.length; i++) { // check for the link - if (children[i].type && children[i].type === 'a') { + if (node.props.children[i].type && node.props.children[i].type === 'a') { node.type = 'stronga'; } } From e7b70bd3cfc338a41a4ebc56b3b3c6b936c355dc Mon Sep 17 00:00:00 2001 From: Tasos Maroudas Date: Fri, 1 Feb 2019 02:38:22 +0200 Subject: [PATCH 3/4] Apply fix for variable type in for loops of stronga --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index cef402b..b6720a7 100644 --- a/index.js +++ b/index.js @@ -181,7 +181,7 @@ class Markdown extends Component { let extras = Utils.concatStyles(null, styles.strongLink); // remove the empty spaces (" ") which is the result of the bold markdown charactes from node's children - for (const i = 0; i < node.props.children.length; i++) { + for (let i = 0; i < node.props.children.length; i++) { if (node.props.children[i] === " ") { delete node.props.children[i]; } @@ -239,7 +239,7 @@ class Markdown extends Component { // check when a strong node hides a link inside if (node.type === 'strong' && typeof(node.props.children) === 'object' && node.props.children.length > 0) { - for (const i = 0; i < node.props.children.length; i++) { + for (let i = 0; i < node.props.children.length; i++) { // check for the link if (children[i].type && children[i].type === 'a') { node.type = 'stronga'; From 087bda98d753ff7dd6f790bc9d1cc55ac13160e7 Mon Sep 17 00:00:00 2001 From: marudy Date: Mon, 1 Jul 2019 14:50:12 +0300 Subject: [PATCH 4/4] Add support for in app deep links --- index.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index e09e907..d80c42d 100644 --- a/index.js +++ b/index.js @@ -160,17 +160,36 @@ class Markdown extends Component { } renderLink(node, key) { - + const { href } = node.props; + let onPress; const { styles } = this.state; let extras = Utils.concatStyles(null, styles.link); let children = this.renderNodes(node.props.children, key, extras); - if (this.props.renderLink) { - return this.props.renderLink(node.props.href, node.props.title, children); + if (/^http/.test(href)) { + onPress = () => Linking.openURL(node.props.href).catch(() => { }); + + if (this.props.renderLink) { + return this.props.renderLink(node.props.href, node.props.title, children); + } + } else { + const deepLinkedScreen = href.split('?')[0]; + const actualScreen = deepLinkedScreen.split('://')[1]; + + let deepLinkExtras; + const extraData = href.split('?')[1]; + if (extraData) { + const key = extraData.split('=')[0]; + const value = extraData.split('=')[1]; + deepLinkExtras = {}; + deepLinkExtras[key] = value; + } + + onPress = () => this.props.onPress(this.props.componentId, actualScreen, deepLinkExtras); } return ( - Linking.openURL(node.props.href).catch(() => { })}> + {children} );