├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── cli.js ├── fixture.txt ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | - 16 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import process from 'node:process'; 3 | import fs from 'node:fs'; 4 | import getStdin from 'get-stdin'; 5 | import meow from 'meow'; 6 | import urlsMd from './index.js'; 7 | 8 | const cli = meow(` 9 | Usage 10 | $ urls-md 11 | $ cat | urls-md 12 | `, { 13 | importMeta: import.meta, 14 | }); 15 | 16 | const init = async string => { 17 | const urls = await urlsMd(string); 18 | console.log(urls.join('\n\n')); 19 | }; 20 | 21 | if (process.stdin.isTTY) { 22 | if (!cli.input[0]) { 23 | console.error('Input file required'); 24 | process.exit(1); 25 | } 26 | 27 | await init(fs.readFileSync(cli.input[0], 'utf8')); 28 | } else { 29 | const stdin = await getStdin(); 30 | await init(stdin); 31 | } 32 | -------------------------------------------------------------------------------- /fixture.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et http://updates.html5rocks.com/2014/01/Yo-Polymer-A-Whirlwind-Tour-Of-Web-Component-Tooling magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. https://sindresorhus.com/blog/small-focused-modules Nulla consequat massa quis enim. https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Octicons-mark-github.svg/1024px-Octicons-mark-github.svg.png 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import getUrls from 'get-urls'; 2 | import articleTitle from 'article-title'; 3 | import got from 'got'; 4 | 5 | export default async function urlsMd(string) { 6 | const urls = getUrls(string); 7 | 8 | if (urls.length === 0) { 9 | throw new Error('No URLs found'); 10 | } 11 | 12 | return Promise.all( 13 | [...urls].map(async url => { 14 | let response; 15 | try { 16 | response = await got(url); 17 | } catch (error) { 18 | if (error.code === 'ENOTFOUND') { 19 | throw new Error(`Could not resolve ${url}`); 20 | } 21 | 22 | throw error; 23 | } 24 | 25 | if (response.headers['content-type'].toLowerCase().startsWith('image/')) { 26 | return `![](${url})`; 27 | } 28 | 29 | const title = articleTitle(response.body) ?? url; 30 | return `[${title}](${url})`; 31 | }), 32 | ); 33 | } 34 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "urls-md", 3 | "version": "5.0.0", 4 | "description": "Convert URLs to Markdown links and images: Extracts URLs from text → Gets their article title → Creates Markdown links", 5 | "license": "MIT", 6 | "repository": "sindresorhus/urls-md", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "bin": "cli.js", 16 | "engines": { 17 | "node": ">=16" 18 | }, 19 | "scripts": { 20 | "test": "xo && ava" 21 | }, 22 | "files": [ 23 | "index.js", 24 | "cli.js" 25 | ], 26 | "keywords": [ 27 | "cli-app", 28 | "cli", 29 | "bin", 30 | "link", 31 | "links", 32 | "dump", 33 | "text", 34 | "string", 35 | "url", 36 | "urls", 37 | "convert", 38 | "extract", 39 | "get", 40 | "markdown", 41 | "md", 42 | "article", 43 | "title", 44 | "image", 45 | "images" 46 | ], 47 | "dependencies": { 48 | "article-title": "^4.1.0", 49 | "get-stdin": "^9.0.0", 50 | "get-urls": "^11.0.0", 51 | "got": "^12.6.0", 52 | "meow": "^12.0.1" 53 | }, 54 | "devDependencies": { 55 | "ava": "^5.2.0", 56 | "xo": "^0.54.2" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # urls-md 2 | 3 | > Convert URLs to Markdown links and images 4 | 5 | [Extracts URLs from text](https://github.com/sindresorhus/get-urls) → [Gets their article title](https://github.com/sindresorhus/article-title) → Creates Markdown links and images 6 | 7 | Useful for when you have a linkdump and want them in Markdown. 8 | 9 | ###### From 10 | 11 | ``` 12 | Lorem ipsum dolor sit amet 13 | http://updates.html5rocks.com/2014/01/Yo-Polymer-A-Whirlwind-Tour-Of-Web-Component-Tooling 14 | Magnis dis parturient montes. 15 | Lorem http://codelittle.com/tag/yeoman/ 16 | https://github.global.ssl.fastly.net/images/modules/logos_page/GitHub-Mark.png 17 | ``` 18 | 19 | ###### To 20 | 21 | ``` 22 | [Yo Polymer – A Whirlwind Tour Of Web Component Tooling](http://updates.html5rocks.com/2014/01/Yo-Polymer-A-Whirlwind-Tour-Of-Web-Component-Tooling) 23 | 24 | [How To Use Yeoman](http://codelittle.com/tag/yeoman/) 25 | 26 | ![](https://github.global.ssl.fastly.net/images/modules/logos_page/GitHub-Mark.png) 27 | ``` 28 | 29 | ## Install 30 | 31 | ```sh 32 | npm install urls-md 33 | ``` 34 | 35 | ## Usage 36 | 37 | ```js 38 | import urslMd from 'urls-md'; 39 | 40 | const urls = await urlsMd('Lorem ipsum http://codelittle.com/tag/yeoman/'); 41 | 42 | console.log(urls); 43 | //=> ['[How To Use Yeoman](http://codelittle.com/tag/yeoman/)'] 44 | ``` 45 | 46 | ## API 47 | 48 | ### urlsMd(input) 49 | 50 | #### input 51 | 52 | Type: `string` 53 | 54 | Text to extract Markdown links and images from. 55 | 56 | ## CLI 57 | 58 | ```sh 59 | npm install --global urls-md 60 | ``` 61 | 62 | ```sh 63 | urls-md --help 64 | 65 | Usage 66 | urls-md 67 | cat | urls-md 68 | ``` 69 | 70 | You can also easily run through multiple files using shell scripting. In this example using ZSH syntax: 71 | 72 | ```sh 73 | # Loops through all .txt files in the current directory and outputs the converted files with .md extension 74 | for f (*.txt) { urls-md $f > $f.md } 75 | ``` 76 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import test from 'ava'; 3 | import urlsMd from './index.js'; 4 | 5 | test('main', async t => { 6 | t.deepEqual(await urlsMd(fs.readFileSync('fixture.txt', 'utf8')), [ 7 | '[Yo Polymer – A Whirlwind Tour Of Web Component Tooling](http://updates.html5rocks.com/2014/01/Yo-Polymer-A-Whirlwind-Tour-Of-Web-Component-Tooling)', 8 | '[Small Focused Modules](https://sindresorhus.com/blog/small-focused-modules)', 9 | '![](https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Octicons-mark-github.svg/1024px-Octicons-mark-github.svg.png)', 10 | ]); 11 | }); 12 | --------------------------------------------------------------------------------