Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
hgezim committed May 4, 2020
1 parent 15bd87c commit 10ef765
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Transform your boring old blue-underlined links in your Gatsby blog:

![Gatsby Remakr Link Before and After](docs/preview.png)

## Background
## Motivation

I've always admired how good links look in Slack. Instead of showing you a measly underlined blue link, Slack show you this:

Expand Down
15 changes: 7 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { unfurl } from 'unfurl.js'
const fs = require('fs')
const path = require('path')
import { selectPossibleOembedLinkNodes } from './selectPossibleOembedLinkNodes'
import { selectPossibleLinkNodes } from './selectPossibleLinkNodes'
import logResults from './logResults.js'
import { tranformsLinkNodeToOembedNode } from './transformLinkToOembedNode'
import { IOembed } from './interfaces'
import { tranformsLinkNodeToUnfurledNode } from './transformLinkToUnfurledNode'
import { MetadataInterface } from './interfaces'
import { Node } from 'unist'

export default async (
Expand All @@ -29,7 +29,7 @@ export default async (
fs.writeFileSync(options.processedUrlsFile, '{}')
}

const nodes = selectPossibleOembedLinkNodes(markdownAST)
const nodes = selectPossibleLinkNodes(markdownAST)

if (nodes.length > 0) {
const results = await Promise.all(
Expand All @@ -42,15 +42,15 @@ export default async (
logResults(results, markdownNode, reporter)
}
} catch (error) {
reporter.error(`gatsby-remark-oembed: Error processing links`, error)
reporter.error(`gatsby-remark-link-unfurl: Error processing links`, error)
}
}

// For each node this is the process
const processNode = async (
node: any,
options: any,
processedUrl: { [key: string]: IOembed }
processedUrl: { [key: string]: MetadataInterface }
): Promise<Node> => {
try {
const metaData = await unfurl(node.url)
Expand Down Expand Up @@ -78,11 +78,10 @@ const processNode = async (
metaData.open_graph?.site_name ||
metaData.twitter_card?.site ||
undefined,
// iframe
}
}

return tranformsLinkNodeToOembedNode(node, processedUrl[node.url])
return tranformsLinkNodeToUnfurledNode(node, processedUrl[node.url])
} catch (error) {
error.url = node.url
return error
Expand Down
2 changes: 1 addition & 1 deletion interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface IOembed {
export interface MetadataInterface {
title?: string
description?: string
url: string
Expand Down
4 changes: 2 additions & 2 deletions logResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const logResults = (results, node, reporter) => {
if (result instanceof Error) {
failedEmbedsCount++;
reporter.error(
`gatsby-remark-oembed: Error embedding ${result.url}`,
`gatsby-remark-link-unfurl: Error embedding ${result.url}`,
result
);
} else if (result) {
Expand All @@ -18,7 +18,7 @@ const logResults = (results, node, reporter) => {
}
}

message += `gatsby-remark-oembed:`;
message += `gatsby-remark-link-unfurl:`;
message += ` Successful embeds: ${successfulEmbedsCount}`;

if (failedEmbedsCount > 0) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-remark-link-unfurl",
"version": "1.0.0",
"version": "1.0.2",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { select, selectAll } from 'unist-util-select'
import { Node } from 'unist'

export const selectPossibleOembedLinkNodes = markdownAST => {
export const selectPossibleLinkNodes = markdownAST => {
return selectAll('paragraph link:only-child', markdownAST)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IOembed } from './interfaces'
import { MetadataInterface } from './interfaces'
import { Node } from 'unist'
import * as mustache from 'mustache'
export const tranformsLinkNodeToOembedNode = (node: Node, card: IOembed) => {
export const tranformsLinkNodeToUnfurledNode = (node: Node, card: MetadataInterface) => {
node.type = 'html'
const template = `<a class='gatsby-remark-link-unfurl__container' href='{{card.url}}' target='_blank' title='{{card.title}}'>
<div class='gatsby-remark-link-unfurl__media' style='background-image: url({{card.image.url}}'></div>
Expand Down

0 comments on commit 10ef765

Please sign in to comment.