Comparing version 3.0.1 to 3.1.0
132
index.js
@@ -11,3 +11,3 @@ const { parse: parseUrl, format: formatUrl } = require("url"); | ||
function getTwitterHashUrl(query, source) { | ||
const getTwitterHashUrl = (query, source) => { | ||
const parameters = { q: `#${query}` }; | ||
@@ -19,22 +19,23 @@ if (undefined !== source) { | ||
return getTwitterUrl("search", parameters); | ||
} | ||
}; | ||
function escapeMarkdownPart(input) { | ||
return [ | ||
// escaping symbols: # * ( ) [ ] _ ` | ||
[/([\#\*\(\)\[\]\_\`\\])/g, "\\$1"], | ||
const escapeReplacements = [ | ||
// escaping symbols: # * ( ) [ ] _ ` | ||
[/([#*()\[\]_`\\])/g, "\\$1"], | ||
// escaping less and more signs | ||
[/\</g, "<"], | ||
[/\>/g, ">"], | ||
// escaping less and more signs | ||
[/</g, "<"], | ||
[/>/g, ">"], | ||
// convert line break into markdown hardbrake | ||
[/\n/g, " \n"] | ||
].reduce( | ||
// convert line break into markdown hard break | ||
[/\n/g, " \n"], | ||
]; | ||
const escapeMarkdownPart = (input) => | ||
escapeReplacements.reduce( | ||
(input, [replaceFrom, replaceTo]) => input.replace(replaceFrom, replaceTo), | ||
input | ||
); | ||
} | ||
const escapeMarkdown = input => | ||
const escapeMarkdown = (input) => | ||
escapeMarkdownPart(input) | ||
@@ -44,42 +45,50 @@ // escaping period after number at the string start | ||
function renderQuote(data) { | ||
const content = module | ||
.exports(data) | ||
.split("\n") | ||
.map(row => `> ${row}`) | ||
.join("\n"); | ||
const renderMarkdownLink = (name, url, title) => { | ||
const parts = [url]; | ||
if (title) { | ||
parts.push(`"${title}"`); | ||
} | ||
return ` | ||
${content}`; | ||
} | ||
return `[${name}](${parts.join(" ")})`; | ||
}; | ||
function renderEntityMention(data) { | ||
const url = getTwitterUrl(data.screen_name); | ||
const renderMarkdownLinkName = (name, prefix) => { | ||
const data = [escapeMarkdownPart(name)]; | ||
if (prefix) { | ||
data.unshift(prefix); | ||
} | ||
return `[@${escapeMarkdownPart(data.screen_name)}](${url} "${data.name}")`; | ||
} | ||
return data.join(""); | ||
}; | ||
function renderEntityMedia(data) { | ||
return `[${escapeMarkdownPart(data.display_url)}](${data.url})`; | ||
} | ||
const renderEntityMedia = (data) => | ||
renderMarkdownLink(data.display_url, data.url); | ||
function renderEntityHashtag(data) { | ||
const url = getTwitterHashUrl(data.text); | ||
const renderEntityMention = (data) => | ||
renderMarkdownLink( | ||
renderMarkdownLinkName(data.screen_name, "@"), | ||
getTwitterUrl(data.screen_name), | ||
data.name | ||
); | ||
return `[#${escapeMarkdownPart(data.text)}](${url})`; | ||
} | ||
const renderEntityHashtag = (data) => | ||
renderMarkdownLink( | ||
renderMarkdownLinkName(data.text, "#"), | ||
getTwitterHashUrl(data.text) | ||
); | ||
function renderEntitySymbol(data) { | ||
const url = getTwitterHashUrl(data.text, "ctag"); | ||
const renderEntitySymbol = (data) => | ||
renderMarkdownLink( | ||
renderMarkdownLinkName(data.text, "$"), | ||
getTwitterHashUrl(data.text, "ctag") | ||
); | ||
return `[$${escapeMarkdownPart(data.text)}](${url})`; | ||
} | ||
function renderEntityUrl(data) { | ||
return `[${escapeMarkdownPart(data.display_url)}](${data.url} "${ | ||
const renderEntityUrl = (data) => | ||
renderMarkdownLink( | ||
renderMarkdownLinkName(data.display_url), | ||
data.url, | ||
data.expanded_url | ||
}")`; | ||
} | ||
); | ||
function renderEntity(type, data) { | ||
const renderEntity = (type, data) => { | ||
if (data.skip) return; | ||
@@ -99,5 +108,5 @@ | ||
default: | ||
return null; | ||
return null; // not undefined! | ||
} | ||
} | ||
}; | ||
@@ -118,3 +127,3 @@ const unicodeCharAt = (string, index) => { | ||
const unicodeSlice = (string, start, end = string.length) => { | ||
if (start == end) { | ||
if (start === end) { | ||
return ""; | ||
@@ -141,4 +150,3 @@ } | ||
function processText(text, replacements) { | ||
let processed = text; | ||
const processText = (text, replacements) => { | ||
let lastPos = 0; | ||
@@ -164,5 +172,5 @@ | ||
return parts.join(""); | ||
} | ||
}; | ||
function getStatusIdFromUrlEntity(entity) { | ||
const getStatusIdFromUrlEntity = (entity) => { | ||
const { expanded_url: url } = entity; | ||
@@ -176,5 +184,5 @@ if (!url) return; | ||
return statusMatch && statusMatch[1]; | ||
} | ||
}; | ||
module.exports = function(tweet = {}) { | ||
const renderTweet = (tweet = {}) => { | ||
const source = tweet.extended_tweet || tweet; | ||
@@ -188,3 +196,3 @@ | ||
const replacements = []; | ||
Object.keys(entities).forEach(entityKey => { | ||
Object.keys(entities).forEach((entityKey) => { | ||
const entityList = entities[entityKey]; | ||
@@ -203,3 +211,3 @@ | ||
...entityList | ||
.map(entity => { | ||
.map((entity) => { | ||
const [start, end] = entity.indices; | ||
@@ -209,3 +217,3 @@ return [renderEntity(entityKey, entity), start, end]; | ||
// do not add anything unknown | ||
.filter(data => null !== data[0]) | ||
.filter((data) => null !== data[0]) | ||
); | ||
@@ -221,1 +229,13 @@ }); | ||
}; | ||
const renderQuote = (data) => { | ||
const content = renderTweet(data) | ||
.split("\n") | ||
.map((row) => `> ${row}`) | ||
.join("\n"); | ||
return ` | ||
${content}`; | ||
}; | ||
module.exports = renderTweet; |
{ | ||
"name": "tweet.md", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Render tweet to markdown", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"test": "jest", | ||
"travis": "npm run coverage; cat ./coverage/lcov.info | coveralls" | ||
"prepare": "husky install" | ||
}, | ||
@@ -36,17 +36,11 @@ "files": [ | ||
"devDependencies": { | ||
"coveralls": "^3.0.9", | ||
"husky": "^4.2.1", | ||
"jest": "^25.1.0", | ||
"prettier": "^1.19.1", | ||
"pretty-quick": "^2.0.1" | ||
"coveralls": "^3.1.0", | ||
"husky": "^8.0.1", | ||
"jest": "^29.1.2", | ||
"prettier": "^2.2.0", | ||
"pretty-quick": "^3.1.0" | ||
}, | ||
"engines": { | ||
"node": ">=8.0.0" | ||
}, | ||
"dependencies": {}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
"node": ">=10.0.0" | ||
} | ||
} |
# Tweet markdown renderer | ||
[![npm](https://img.shields.io/npm/v/tweet.md.svg)](https://www.npmjs.com/package/tweet.md) | ||
[![Travis](https://img.shields.io/travis/silentroach/tweet.md.svg?label=travis)](https://travis-ci.org/silentroach/tweet.md) | ||
[![Build](https://img.shields.io/github/workflow/status/silentroach/tweet.md/Test.svg)](https://github.com/silentroach/tweet.md/actions?query=workflow%3ATest) | ||
[![Coveralls](https://img.shields.io/coveralls/silentroach/tweet.md.svg?label=coverage)](https://coveralls.io/r/silentroach/tweet.md) | ||
@@ -23,3 +23,3 @@ [![install size](https://packagephobia.now.sh/badge?p=tweet.md)](https://packagephobia.now.sh/result?p=tweet.md) | ||
```js | ||
var render = require('tweet.md'); | ||
var render = require("tweet.md"); | ||
var output = render(tweetObjectFromAPI); | ||
@@ -58,3 +58,10 @@ ``` | ||
Keep your Tweets 💯 by adding emojis every day, but especially on [#WorldEmojiDay](https://twitter.com/search?q=%23WorldEmojiDay)! 💪😂👯 | ||
Twitter libraries are a great way to use our API. Did you know: | ||
✨ 100+ libraries support the Twitter API | ||
✨ In 25+ programming languages 🐍☕️♦️ | ||
Explore our curated list of community built libraries 📚 | ||
[developer.twitter.com/en/docs/develo…](https://t.co/mgXiWmt9SY "https://developer.twitter.com/en/docs/developer-utilities/twitter-libraries") | ||
- - - | ||
@@ -61,0 +68,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9856
179
77