diff --git a/utils/markdown-test/Markdown.jsx b/utils/markdown-test/Markdown.jsx index 839be6f..6679b66 100644 --- a/utils/markdown-test/Markdown.jsx +++ b/utils/markdown-test/Markdown.jsx @@ -3,34 +3,6 @@ */ import React from 'react'; -/* - * gets a descriptive text of the domain of the link - * Example: - * https://www.youtube.com/watch?v=G8APgeFfkAk returns 'youtube' - * http://www.google.at returns 'google.at' - * (www. and .com are split) - */ -function getLinkDesc(link) { - let domainStart = link.indexOf('://') + 3; - if (domainStart < 3) { - domainStart = 0; - } - if (link.startsWith('www.', domainStart)) { - domainStart += 4; - } - let domainEnd = link.indexOf('/', domainStart); - if (domainEnd === -1) { - domainEnd = link.length; - } - if (link.endsWith('.com', domainEnd)) { - domainEnd -= 4; - } - if (domainEnd <= domainStart) { - return link; - } - return link.slice(domainStart, domainEnd); -} - const MarkdownParagraph = ({ pArray }) => pArray.map((part) => { if (!Array.isArray(part)) { return part; diff --git a/utils/markdown-test/MdLink.jsx b/utils/markdown-test/MdLink.jsx new file mode 100644 index 0000000..f9af760 --- /dev/null +++ b/utils/markdown-test/MdLink.jsx @@ -0,0 +1,61 @@ +/* + * Renders a markdown link + * Also provides previews + * Links are assumed to start with protocol (http:// etc.) + */ +import React from 'react'; + +/* + * gets a descriptive text of the domain of the link + * Example: + * https://www.youtube.com/watch?v=G8APgeFfkAk returns 'youtube' + * http://www.google.at returns 'google.at' + * (www. and .com are split) + */ +function getLinkDesc(link) { + let domainStart = link.indexOf('://') + 3; + if (domainStart < 3) { + domainStart = 0; + } + if (link.startsWith('www.', domainStart)) { + domainStart += 4; + } + let domainEnd = link.indexOf('/', domainStart); + if (domainEnd === -1) { + domainEnd = link.length; + } + if (link.endsWith('.com', domainEnd)) { + domainEnd -= 4; + } + if (domainEnd <= domainStart) { + return link; + } + return link.slice(domainStart, domainEnd); +} + +/* + * try to get extension out of link + */ +function getExt(link) { + let paramStart = link.indexOf('&'); + if (paramStart === -1) { + paramStart = link.length; + } + let posDot = paramStart - 1; + for (;posDot >= 0 && link[posDot] !== '.'; posDot -= 1) { + if (link[posDot] === '/') { + return null; + } + } + if (paramStart - posDot > 4) { + return null; + } + return link.slice(posDot, paramStart); +} + +const MdLink = ({ href, title, type }) => { + const desc = getLinkDesc(href); +
+}; + +export default Reace.memo(MdLink);