├── .babelrc ├── .gitignore ├── .npmignore ├── README.md ├── lib └── index.js ├── package.json └── test ├── article.amp.html ├── article.apple-news.json ├── article.html ├── article.instant-article.html └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /dist 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/micnews/distro/63ffaafd7b0bdfe8ca78dcd878171c76b1558339/.npmignore -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Distro.Mic 2 | Transform article HTML for [Facebook Instant Articles](https://developers.facebook.com/docs/instant-articles), [Apple News](https://developer.apple.com/library/ios/documentation/General/Conceptual/Apple_News_Format_Ref/AppleNewsFormat.html) and [Google AMP](https://www.ampproject.org/docs/get_started/about-amp.html). 3 | 4 | Note, this module is designed to transform article body only, other elements of the article page like header, byline and custom stylesheets should be added separately for each format. 5 | 6 | ## USAGE 7 | 8 | Install using npm: 9 | 10 | ```bash 11 | npm install distro-mic 12 | ``` 13 | 14 | ```js 15 | var format = require('distro-mic').format; 16 | var html = '

Article HTML

' 17 | 18 | var output = format(html); 19 | ``` 20 | 21 | Output: 22 | 23 | ```js 24 | { 25 | amp: '

Article HTML

', 26 | instantArticle: '

Article HTML

', 27 | appleNews: { 28 | article: [{ 29 | text: 'Article HTML\n', 30 | additions: [], 31 | inlineTextStyles: [], 32 | role: 'body', 33 | layout: 'bodyLayout' 34 | }], 35 | bundlesToUrls: {} 36 | } 37 | } 38 | 39 | ``` 40 | 41 | ## API ENDPOINT 42 | 43 | Distro is also available via API endpoint. 44 | 45 | ``` 46 | POST https://distro.mic.com/1.0/format 47 | ``` 48 | 49 | **Post data:** 50 | 51 | *Document HTML to transform* 52 | 53 | **Query parameters:** 54 | 55 | *output* - one of `apple-news`, `instant-article` or `amp`. Return output for specified platform only. Optional. 56 | 57 | ## LICENSE 58 | 59 | MIT 60 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | import createHtmlToArticleJson from 'html-to-article-json'; 2 | import articleJsonToAmp from 'article-json-to-amp'; 3 | import articleJsonToIA from 'article-json-to-fbia'; 4 | import articleJsonToAppleNews from 'article-json-to-apple-news'; 5 | 6 | const htmlToArticleJson = createHtmlToArticleJson({}); 7 | 8 | export function format (html, opts = {}) { 9 | const articleJson = htmlToArticleJson(html); 10 | 11 | const amp = articleJsonToAmp(articleJson); 12 | const instantArticle = articleJsonToIA(articleJson); 13 | const appleNews = articleJsonToAppleNews({ 14 | title: 'Untitled', 15 | author: { 16 | name: '' 17 | }, 18 | body: articleJson 19 | }, { 20 | identifier: 'ID' 21 | }); 22 | 23 | return { 24 | amp, 25 | instantArticle, 26 | appleNews: { 27 | article: appleNews.article.components[1].components /* body only */, 28 | bundlesToUrls: appleNews.bundlesToUrls 29 | } 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "distro-mic", 3 | "version": "1.0.0", 4 | "description": "Automatically transform article HTML for third-party platforms such as Facebook Instant Articles, Apple News and Google AMP", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "lint": "semistandard | snazzy", 8 | "test": "babel-node test/index.js && npm run lint", 9 | "build": "babel lib --out-dir dist", 10 | "watch": "babel lib --out-dir dist --watch", 11 | "prepublish": "npm run build" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/micnews/distro.git" 16 | }, 17 | "author": "mic.com", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/micnews/distro/issues" 21 | }, 22 | "homepage": "https://github.com/micnews/distro#readme", 23 | "devDependencies": { 24 | "babel-cli": "^6.4.5", 25 | "babel-core": "^6.5.2", 26 | "babel-preset-es2015": "^6.3.13", 27 | "semistandard": "^9.0.0", 28 | "snazzy": "^5.0.0", 29 | "tape": "^4.5.1" 30 | }, 31 | "dependencies": { 32 | "article-json-to-amp": "^1.7.1", 33 | "article-json-to-apple-news": "^3.2.0", 34 | "article-json-to-fbia": "^1.1.0", 35 | "html-to-article-json": "^1.13.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/article.amp.html: -------------------------------------------------------------------------------- 1 |
2 |

Universe Might Be Expanding Faster Than We Thought, Scientists Say

3 |

Astronomers just announced the universe might be expanding up to 9% faster than we thought.

4 |

It's a surprising insight that could put us one step closer to finally figuring out what the hell dark energy and dark matter are. Or it could mean that we've gotten something fundamentally wrong in our understanding of physics, perhaps even poking a hole in Einstein's theory of gravity.

5 |
6 | 7 |
Universe Might Be Expanding Faster Than We Thought, Scientists Say
8 |
9 |
10 | 11 |
Astronomers used the brightness of Cepheid stars and the distance of Type 1a supernovas to measure how fast the universe is expanding.
12 |
13 |

Source: https://mic.com

14 |
15 | -------------------------------------------------------------------------------- /test/article.apple-news.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "text": "Universe Might Be Expanding Faster Than We Thought, Scientists Say", 4 | "additions": [], 5 | "inlineTextStyles": [], 6 | "role": "heading1", 7 | "layout": "bodyLayout" 8 | }, 9 | { 10 | "text": "Astronomers just announced the universe might be expanding up to 9% faster than we thought.\n", 11 | "additions": [ 12 | { 13 | "type": "link", 14 | "rangeStart": 59, 15 | "rangeLength": 8, 16 | "URL": "http://hubblesite.org/newscenter/archive/releases/2016/17/text/" 17 | } 18 | ], 19 | "inlineTextStyles": [ 20 | { 21 | "rangeStart": 59, 22 | "rangeLength": 8, 23 | "textStyle": "bodyLinkTextStyle" 24 | } 25 | ], 26 | "role": "body", 27 | "layout": "bodyLayout" 28 | }, 29 | { 30 | "text": "It's a surprising insight that could put us one step closer to finally figuring out what the hell dark energy and dark matter are. Or it could mean that we've gotten something fundamentally wrong in our understanding of physics, perhaps even poking a hole in Einstein's theory of gravity.\n", 31 | "additions": [ 32 | { 33 | "type": "link", 34 | "rangeStart": 98, 35 | "rangeLength": 27, 36 | "URL": "http://science.nasa.gov/astrophysics/focus-areas/what-is-dark-energy/" 37 | } 38 | ], 39 | "inlineTextStyles": [ 40 | { 41 | "rangeStart": 98, 42 | "rangeLength": 27, 43 | "textStyle": "bodyLinkTextStyle" 44 | } 45 | ], 46 | "role": "body", 47 | "layout": "bodyLayout" 48 | }, 49 | { 50 | "role": "container", 51 | "components": [ 52 | { 53 | "role": "photo", 54 | "URL": "bundle://image-0.jpg", 55 | "style": "embedMediaStyle", 56 | "layout": "embedMediaLayout", 57 | "caption": { 58 | "text": "Universe Might Be Expanding Faster Than We Thought, Scientists Say\n", 59 | "additions": [], 60 | "inlineTextStyles": [], 61 | "textStyle": "embedCaptionTextStyle" 62 | } 63 | } 64 | ], 65 | "layout": "embedLayout", 66 | "style": "embedStyle" 67 | }, 68 | { 69 | "role": "container", 70 | "components": [ 71 | { 72 | "role": "photo", 73 | "URL": "bundle://image-1.jpg", 74 | "style": "embedMediaStyle", 75 | "layout": "embedMediaLayout", 76 | "caption": { 77 | "text": "Astronomers used the brightness of Cepheid stars and the distance of Type 1a supernovas to measure how fast the universe is expanding.\n", 78 | "additions": [], 79 | "inlineTextStyles": [], 80 | "textStyle": "embedCaptionTextStyle" 81 | } 82 | } 83 | ], 84 | "layout": "embedLayout", 85 | "style": "embedStyle" 86 | }, 87 | { 88 | "text": "Source: https://mic.com\n", 89 | "additions": [], 90 | "inlineTextStyles": [ 91 | { 92 | "rangeStart": 0, 93 | "rangeLength": 23, 94 | "textStyle": "bodyItalicStyle" 95 | } 96 | ], 97 | "role": "body", 98 | "layout": "bodyLayout" 99 | } 100 | ] 101 | -------------------------------------------------------------------------------- /test/article.html: -------------------------------------------------------------------------------- 1 | 2 |

Universe Might Be Expanding Faster Than We Thought, Scientists Say

3 | 4 | 5 |

Astronomers just announced the universe might be 6 | expanding up to 9% faster 7 | than we thought.

8 | 9 | 10 |

It's a surprising insight that could put us one step closer to finally figuring out what the 11 | hell dark energy and dark matter are. 12 | Or it could mean that we've gotten something fundamentally wrong in our understanding of physics, 13 | perhaps even poking a hole in Einstein's theory of gravity.

14 | 15 | 16 | Universe Might Be Expanding Faster Than We Thought, Scientists Say 20 | 21 | 22 |
23 | Universe Might Be Expanding Faster Than We Thought, Scientists Say 27 |
Astronomers used the brightness of Cepheid stars and the distance of Type 1a supernovas to measure how fast the universe is expanding.
28 |
Source: NASA, ESA, A. Feild (STScI), and A. Riess (STScI/JHU)
29 |
30 | 31 | 32 |

Source: https://mic.com

33 | -------------------------------------------------------------------------------- /test/article.instant-article.html: -------------------------------------------------------------------------------- 1 |
2 |

Universe Might Be Expanding Faster Than We Thought, Scientists Say

3 |

Astronomers just announced the universe might be expanding up to 9% faster than we thought.

4 |

It's a surprising insight that could put us one step closer to finally figuring out what the hell dark energy and dark matter are. Or it could mean that we've gotten something fundamentally wrong in our understanding of physics, perhaps even poking a hole in Einstein's theory of gravity.

5 |
6 |
Universe Might Be Expanding Faster Than We Thought, Scientists Say
7 |
Astronomers used the brightness of Cepheid stars and the distance of Type 1a supernovas to measure how fast the universe is expanding.
8 |
9 |

Source: https://mic.com

10 |
11 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | import test from 'tape'; 2 | import fs from 'fs'; 3 | import path from 'path'; 4 | import {format} from '../lib'; 5 | 6 | function clean (s) { 7 | return s.replace(/\n\r?\s*/g, ''); 8 | } 9 | 10 | test('input html', t => { 11 | const name = path.resolve(__dirname, 'article'); 12 | const input = fs.readFileSync(`${name}.html`, 'utf8'); 13 | const expected = { 14 | errors: [], 15 | warnings: [], 16 | amp: clean(fs.readFileSync(`${name}.amp.html`, 'utf8')), 17 | instantArticle: clean(fs.readFileSync(`${name}.instant-article.html`, 'utf8')), 18 | appleNews: JSON.parse(fs.readFileSync(`${name}.apple-news.json`, 'utf8')) 19 | }; 20 | 21 | const expectedBundlesToUrls = { 22 | 'image-0.jpg': '//thumbs.mic.com/MTc5ZmM3ZmNjMCMvSGd5QzRWVHAxQ2lDWklBU2VEdmtUV09QNmpzPS8yM3gzNDM6MzgwOXgyMjEzLzkwMHg0NDUvZmlsdGVyczpxdWFsaXR5KDcwKS9odHRwOi8vczMuYW1hem9uYXdzLmNvbS9wb2xpY3ltaWMtaW1hZ2VzL2Fud2FhdXhha3Ftd2htY3hwNWluM3ZybXV1NGw5ZWZ5bHpsbGNpY2I2dWNtdTRycGU0bmhoa21meGxqZzBxYWwuanBn.jpg', 23 | 'image-1.jpg': '//thumbs.mic.com/M2UwMmFkZGY0NyMvanM2cGZ5Y0tuRHJtOUNxYU9UcXpuTTdnYktzPS8zNngyMDI6MTQ2NHg5MDkvOTAweDQ0NS9maWx0ZXJzOnF1YWxpdHkoNzApL2h0dHA6Ly9zMy5hbWF6b25hd3MuY29tL3BvbGljeW1pYy1pbWFnZXMvOXR3enN0b3R2Mmp0emtiendleG5hdnpuYzljZjNrYndhMjB5d2NkcDh3ZHkwbnFmbzJoa2lleWV1dWZyeHRvYy5qcGc.jpg' 24 | }; 25 | 26 | const actual = format(input); 27 | t.deepEqual(actual.amp, expected.amp, 'valid amp article'); 28 | t.deepEqual(actual.instantArticle, expected.instantArticle, 'valid instant article'); 29 | t.deepEqual(actual.appleNews.article, expected.appleNews, 'valid apple news article'); 30 | t.deepEqual(actual.appleNews.bundlesToUrls, expectedBundlesToUrls, 'valid apple news bundlesToUrls'); 31 | t.end(); 32 | }); 33 | --------------------------------------------------------------------------------