loremly
v0.2.0
Published
Lorem ipsum text and placeholder images for JavaScript and the CLI. Exact word and character counts, HTML, Markdown, JSON and JSX output, seeds, SVG placeholders and CC0 photos. Zero dependencies.
Readme
loremly
Lorem ipsum text and placeholder images for JavaScript, TypeScript and the command line.
loremly uses the same engine as loremly.com. The same options and seed give the same text in this package, on the website and from the REST API.
Features
- Exact counts. Ask for paragraphs, sentences, words, list items or an exact number of characters.
- Many formats. Get plain text, HTML, Markdown, JSON or JSX.
- Rich content. Add headings, bold and italic, links, lists, quotes, code blocks and tables.
- Word themes. Choose
latin,cicero,english,corporate,tech,foodie,space, or use your own words. - Seeds. Pass a seed and get the same output every time. This is useful for tests, snapshots and shared mockups.
- Presets. Use ready-made setups such as
blog,meta,headlineandhtml-lorem-ipsum. - Images. Make solid-colour placeholders and generated illustrations as SVG, or as PNG, JPG and WebP in Node. Get URLs for real CC0 photos.
- CLI. Run
npx loremlyto get text or image files without writing code. - Runs anywhere. No dependencies. Works in Node 20+, browsers, Deno, Bun and edge runtimes. Ships ESM, CommonJS and TypeScript types.
Installation
npm install loremlyOnly install sharp if you need PNG, JPG or WebP files in Node:
npm install sharpQuick start
import { paragraphs, sentences, words, characters } from "loremly";
paragraphs(3); // 3 paragraphs of Latin text
words(5, { seed: "demo" }); // => "Lorem ipsum dolor sit amet."
sentences(1, { flavor: "tech", seed: "demo" }); // => "Ship the MVP, deploy to the edge, iterate fast."
characters(40, { seed: "demo" }); // => "Lorem ipsum dolor sit amet, consectetur."import { placeholder, photoUrl } from "loremly";
placeholder({ size: "600x400" }); // SVG string: a grey box with the label "600 × 400"
photoUrl({ size: "800x600", category: "food", seed: "menu" });
// => "https://www.loremly.com/api/v1/photo?w=800&h=600&seed=menu&category=food"CommonJS works too:
const { paragraphs } = require("loremly");Usage
Get the same text every time
Without a seed, every call returns new text. With a seed, the result is always the same.
words(5, { seed: "demo" }); // => "Lorem ipsum dolor sit amet."
words(5, { seed: "demo" }); // => "Lorem ipsum dolor sit amet." (same again)
words(5); // different on every callSkip the "Lorem ipsum" opening
By default the text starts with "Lorem ipsum dolor sit amet". Turn this off with startWith: false.
words(5, { seed: "demo", startWith: false }); // => "Dolor nibh porttitor eleifend libero."Choose an output format
sentences(2, { format: "json", seed: "demo" });
// => '[\n "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",\n "Sit ornare semper ..."\n]'
listItems(3, { format: "markdown", seed: "demo" });
// => "- Lorem ipsum dolor sit amet, consectetur adipiscing elit\n- Sagittis egestas volutpat faucibus\n- Amet hendrerit semper dignissim"
sentences(1, { format: "jsx", seed: "demo" });
// => "<>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>\n</>"Formats: text (default), html, markdown, json, jsx.
Add headings, lists and other elements
Elements look best with the html or markdown format. In plain text, a heading is a single line, a list uses • bullets and a table uses tabs.
import { lorem } from "loremly";
lorem({
unit: "paragraphs",
count: 3,
format: "html",
elements: ["headings", "emphasis", "links", "ul", "blockquote"],
});Elements: headings, emphasis, links, ul, ol, blockquote, code, table.
Use a different word theme
sentences(2, { flavor: "english", seed: "demo" });
// => "This is placeholder text for your next great idea. Teams grow toward the river?"
words(6, { flavor: "custom", customWords: ["pixel", "grid", "kerning"], seed: "demo" });
// => "Kerning grid kerning pixel grid kerning."customWords also accepts one string, split on spaces and commas: "pixel, grid kerning".
Start from a preset
A preset fills in the options for you. Any option you pass yourself wins.
lorem({ preset: "blog" }); // HTML article with headings, links, a list and a quote
lorem({ preset: "meta", seed: "demo" }); // exactly 155 characters, for a meta description
lorem({ preset: "headline", seed: "demo" }); // => "Dolor nibh porttitor eleifend libero pulvinar officia."
lorem({ preset: "blog", count: 2 }); // the blog preset with 2 paragraphs instead of 5Run npx loremly list presets to see all 20 presets, or read TEXT_PRESETS in code.
Get word counts and the seed used
import { loremDetailed } from "loremly";
const { output, stats, options } = loremDetailed({ unit: "words", count: 20, seed: "demo" });
// stats => { words: 20, characters: 148, charactersNoSpaces: 129, sentences: 3, paragraphs: 1, readingSeconds: 5 }
// options.seed => "demo"When you don't pass a seed, options.seed holds the random one that was used. Pass it back to get the same text again.
Link to the same text on loremly.com
import { shareUrl, loremApiUrl } from "loremly";
shareUrl({ unit: "words", count: 50, seed: "abc" });
// => "https://www.loremly.com/?unit=words&count=50&seed=abc" (opens the text in the website generator)
loremApiUrl({ unit: "words", count: 50, seed: "abc" });
// => "https://www.loremly.com/api/v1/lorem?unit=words&count=50&seed=abc" (returns the text from the REST API)Make a placeholder image
Solid placeholders and illustrations are drawn locally as SVG strings. No network is needed.
import { placeholder, illustration, svgDataUri } from "loremly";
placeholder({ size: "600x400" }); // grey box with the label "600 × 400"
placeholder({ size: "social", bg: "#111", fg: "fff", text: "OG image" });
illustration({ size: "1200x800", style: "landscape", seed: "hero" });
illustration({ size: "banner", style: "waves", grayscale: true, blur: 2 });
img.src = svgDataUri(illustration({ size: "300x200", seed: "card-1" }));Use a real photo
Photos come from the loremly.com API, so you get a URL. All photos are CC0 or public domain.
import { photoUrl, illustrationUrl, placeholderUrl, imgTag, markdownImage } from "loremly";
photoUrl({ size: "800x600", category: "food", seed: "menu" }); // same photo every time
photoUrl({ size: "400", category: "city" }); // a new photo on every page load
illustrationUrl({ size: "1200x800", style: "gradient", seed: "x", format: "webp" });
placeholderUrl({ size: "300x200", text: "Logo" });
imgTag(photoUrl({ size: "400x300", seed: "a" }), { width: 400, height: 300, alt: "Placeholder photo" });
// => '<img src="https://www.loremly.com/api/v1/photo?w=400&h=300&seed=a" width="400" height="300" alt="Placeholder photo">'
markdownImage(placeholderUrl({ size: "600x400" }), "Placeholder");
// => ""Save image files in Node
Import from loremly/node to write real image files. It also includes everything from loremly.
import { writeFile } from "node:fs/promises";
import { renderPlaceholder, renderIllustration, fetchPhoto } from "loremly/node";
const og = await renderPlaceholder({ size: "social", text: "Coming soon", format: "png" });
await writeFile("og.png", og.data);
const hero = await renderIllustration({ size: "banner", style: "waves", seed: "hero", format: "webp" });
await writeFile(`hero.${hero.ext}`, hero.data);
const photo = await fetchPhoto({ size: "800x600", category: "food", seed: "menu" });
await writeFile("dish.jpg", photo.data);Each function returns { data, type, ext, width, height }, where data is a Buffer. PNG, JPG and WebP need sharp. SVG does not. fetchPhoto downloads from loremly.com, so it needs internet access.
Use the CLI
# Text
npx loremly # 4 paragraphs
npx loremly 50 words
npx loremly 3 -f tech -F html # 3 paragraphs, tech words, as HTML
npx loremly -p blog -o post.html # blog preset, saved to a file
npx loremly 155 characters --copy # copy to the clipboard
npx loremly 2 -e headings,ul -F markdown -s demo
npx loremly 3 sentences --json # seed, options, stats and output as JSON
npx loremly 5 words -s abc --link --api # share link and API URL
# Images
npx loremly image 600x400 # SVG to stdout
npx loremly image social -t "OG image" -F png -o og.png
npx loremly illustration banner --style waves -s hero -F webp -o hero.webp
npx loremly photo 800x600 --category food -s menu -o dish.jpg
npx loremly photo 400 --category city --as html # <img> tag. Also: --as markdown, --url
# Help
npx loremly list # presets, flavors, styles, categories and sizes
npx loremly <command> --helpText options: -p preset, -f flavor, -F format, -l length, -e elements, -s seed, -w your own words, --no-start, --stats, --json, --link, --api.
If you don't pass -o and the output is a terminal, PNG, JPG and WebP files are saved as loremly-WIDTHxHEIGHT.ext. If you pipe the output, the image data goes to stdout. On Linux, --copy needs wl-clipboard, xclip or xsel.
API
Text
| Function | Returns |
|---|---|
| lorem(options?) | Text as a string. This is also the default export. |
| paragraphs(count?, options?) | That many paragraphs. |
| sentences(count?, options?) | That many sentences. |
| words(count?, options?) | Exactly that many words. |
| listItems(count?, options?) | That many list items. Use format: "html" or "markdown" for list markup. |
| characters(count?, options?) | Text with exactly that many characters. |
| loremDetailed(options?) | { output, stats, options, result }: the text, counts, final options (with the seed) and the block tree. |
| shareUrl(options?, baseUrl?) | A loremly.com link that opens this exact text. |
| loremApiUrl(options?, baseUrl?) | A REST API URL that returns this exact text. |
| resolveOptions(options?) | The final options after presets, defaults and limits are applied. |
| getTextPreset(id) | One preset from TEXT_PRESETS, or undefined. |
Text options (all optional):
| Option | Type | Default | Description |
|---|---|---|---|
| unit | "paragraphs" \| "sentences" \| "words" \| "lists" \| "characters" | "paragraphs" | What count counts. |
| count | number | Depends on unit | How many. See Text limits. |
| flavor | "latin" \| "cicero" \| "english" \| "corporate" \| "tech" \| "foodie" \| "space" \| "custom" | "latin" | Word theme. |
| customWords | string \| string[] | "" | Your own words, used when flavor is "custom". |
| length | "short" \| "medium" \| "long" \| "mixed" | "medium" | Length of sentences and paragraphs. |
| format | "text" \| "html" \| "markdown" \| "json" \| "jsx" | "text" | Output format. |
| elements | string[] | [] | Extra blocks: headings, emphasis, links, ul, ol, blockquote, code, table. |
| startWith | boolean | true | Start with "Lorem ipsum dolor sit amet". |
| seed | string | Random | Same seed, same text. |
| preset | string | None | Start from a preset. Your own options override it. |
Images
| Function | Returns |
|---|---|
| placeholder(options?) | Solid-colour placeholder as an SVG string. |
| illustration(options?) | Generated artwork as an SVG string. |
| photoUrl(options?, baseUrl?) | URL of a real CC0 photo, cropped to size. |
| placeholderUrl(options?, baseUrl?) | API URL for a solid placeholder. |
| illustrationUrl(options?, baseUrl?) | API URL for an illustration. |
| imgTag(src, { width?, height?, alt? }) | An <img> tag with escaped attributes. |
| markdownImage(src, alt?) | A Markdown image. |
| svgDataUri(svg) | A data: URI for an <img> src or CSS background. |
| parseSize(size) | { width, height } from "600x400", "600" or a size preset, or undefined. |
From loremly/node:
| Function | Returns |
|---|---|
| renderPlaceholder(options?) | Promise<ImageFile> as SVG or PNG. |
| renderIllustration(options?) | Promise<ImageFile & { seed, style }> as SVG (default), PNG, JPG or WebP. |
| fetchPhoto(options?, { baseUrl?, fetch?, signal?, timeoutMs? }) | Promise<ImageFile & { seed, url }>. Times out after 30 seconds by default. |
ImageFile is { data: Buffer, type, ext, width, height }.
Image options (all optional):
| Option | Type | Default | Used by | Description |
|---|---|---|---|---|
| size | string | "600" | All | "600x400", "600" (square) or a size preset. |
| width, height | number | 600 | All | Set the size in pixels. They override size. |
| bg | string | "e5e7eb" | Placeholder | Background colour as hex: "eee", "e5e7eb" or "#e5e7eb". |
| fg | string | "6b7280" | Placeholder | Text colour as hex. |
| text | string | The size | Placeholder | Label, for example "Logo". |
| style | "landscape" \| "gradient" \| "soft" \| "waves" \| "shapes" \| "any" | "any" | Illustration | Picture style. "any" picks one from the seed. |
| category | "nature" \| "ocean" \| "city" \| "architecture" \| "food" \| "animals" \| "flowers" \| "forest" \| "any" | "any" | Photo | Photo subject. |
| seed | string | Random | Illustration, photo | Same seed, same picture. |
| grayscale | boolean | false | Illustration, photo | Black and white. |
| blur | number | 0 | Illustration, photo | Blur from 1 to 10. 0 is off. |
| format | string | See description | All | Placeholder: svg, png. Illustration: svg, png, jpg, webp. Photo: jpg, webp, png. |
Size presets:
| Name | Size |
|---|---|
| square | 600 × 600 |
| landscape | 1200 × 800 |
| portrait | 800 × 1200 |
| banner | 1600 × 400 |
| social | 1200 × 630 |
| thumbnail | 300 × 200 |
| avatar | 200 × 200 |
Constants and the low-level engine
loremly also exports the lists and limits it uses: TEXT_PRESETS, FLAVORS, FLAVOR_LIST, UNITS, FORMATS, LENGTHS, ELEMENTS, LIMITS, DEFAULT_OPTIONS, SIZE_PRESETS, PHOTO_CATEGORIES, SCENE_STYLES, IMAGE_LIMITS and DEFAULT_BASE_URL.
The text engine is exported as well: generateLorem, render, stats, parseOptions and serializeOptions. parseOptions and serializeOptions read and write the same URL parameters as the REST API. All types are exported.
If you host your own copy of Loremly, pass its address as the last argument to any URL helper, or as baseUrl to fetchPhoto.
Comparison
| | loremly | lorem-ipsum |
|---|---|---|
| Paragraphs, sentences and words | Yes | Yes |
| Exact character count | Yes | No |
| HTML output | Yes | Yes |
| Markdown, JSON and JSX output | Yes | No |
| Headings, lists, quotes, code and tables | Yes | No |
| Same output from a seed | Yes, pass a seed string | Yes, pass your own random function |
| Word themes | 7 themes, or your own words | Your own words |
| Placeholder images | Yes | No |
| Link to the same text on a website | Yes | No |
| CLI | Yes | Yes |
| Dependencies | 0 | 0 |
Migrating from lorem-ipsum
// Before
import { LoremIpsum } from "lorem-ipsum";
const lorem = new LoremIpsum();
lorem.generateParagraphs(3);
lorem.generateSentences(5);
lorem.generateWords(10);
// After
import { paragraphs, sentences, words } from "loremly";
paragraphs(3);
sentences(5);
words(10);For HTML, pass { format: "html" }. For your own words, pass { flavor: "custom", customWords: [...] }.
Text limits
| Unit | Min | Max | Default |
|---|---|---|---|
| paragraphs | 1 | 100 | 4 |
| sentences | 1 | 500 | 8 |
| words | 1 | 10,000 | 100 |
| lists | 1 | 200 | 6 |
| characters | 1 | 50,000 | 160 |
A count outside the range is moved to the nearest limit. For example, paragraphs(500) returns 100 paragraphs.
Notes and limitations
- Seeds and versions. A seed gives the same text with the same version of loremly. A later version can change seeded output if the text engine changes, so pin the version if your snapshot tests depend on it.
- Image sizes. SVG placeholders go up to 4000 px, PNG placeholders up to 2000 px, and illustrations and photos up to 3000 px. Larger sizes are reduced to the limit.
- Errors. An unknown
presetorsizethrows anErrorthat lists the valid values. Other unknown values, such as a photocategory, fall back to the default. - Blur. Values above 10 are treated as 10.
- Network. Text, SVG placeholders and SVG illustrations are made locally. Photos,
fetchPhotoand the URL helpers use loremly.com. - Bundle size. Text and image functions together add about 15 KB (minified and gzipped) to a browser bundle.
Requirements
- Node.js 20 or later, or a modern browser, Deno, Bun or an edge runtime.
sharp0.33 or later, only for PNG, JPG and WebP files in Node.
License
MIT. Photos served by the loremly.com API are CC0 or public domain.
