{"id":13601691,"url":"https://github.com/sindresorhus/multiline","last_synced_at":"2025-10-22T18:44:55.361Z","repository":{"id":14733432,"uuid":"17454260","full_name":"sindresorhus/multiline","owner":"sindresorhus","description":"Multiline strings in JavaScript","archived":true,"fork":false,"pushed_at":"2021-01-23T17:42:12.000Z","size":35,"stargazers_count":1414,"open_issues_count":0,"forks_count":65,"subscribers_count":36,"default_branch":"main","last_synced_at":"2024-10-29T22:38:28.794Z","etag":null,"topics":["deprecated"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2014-03-05T20:31:03.000Z","updated_at":"2024-10-25T09:59:40.000Z","dependencies_parsed_at":"2022-09-05T17:00:26.609Z","dependency_job_id":null,"html_url":"https://github.com/sindresorhus/multiline","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fmultiline","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fmultiline/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fmultiline/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fmultiline/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/multiline/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234426270,"owners_count":18830880,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deprecated"],"created_at":"2024-08-01T18:01:06.502Z","updated_at":"2025-09-27T10:31:00.337Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["JavaScript","String","String [🔝](#readme)","Web 前端","📦 Legacy \u0026 Inactive Projects","Packages","字符串","Number"],"sub_categories":["Runner","Miscellaneous","运行器","运行器e2e测试"],"readme":"# multiline [![Build Status](https://travis-ci.org/sindresorhus/multiline.svg?branch=master)](https://travis-ci.org/sindresorhus/multiline)\n\n\u003e Multiline strings in JavaScript\n\nNo more string concatenation or array join!\n\n*Use ES2015 [template literals](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) instead whenever possible.*\n\n#### Before\n\n```js\nconst str = '' +\n'\u003c!doctype html\u003e' +\n'\u003chtml\u003e' +\n'\t\u003cbody\u003e' +\n'\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e' +\n'\t\u003c/body\u003e' +\n'\u003c/html\u003e' +\n'';\n```\n\n#### After\n\n```js\nconst str = multiline(()=\u003e{/*\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n*/});\n```\n\n\n## How\n\nIt works by wrapping the text in a block comment, anonymous function, and a function call. The anonymous function is passed into the function call and the contents of the comment extracted.\n\nEven though it's [slower than string concat](http://jsperf.com/multiline), that shouldn't realistically matter as you can still do 2 million of those a second. Convenience over micro performance always.\n\n\n## Install\n\n```\n$ npm install multiline\n```\n\n\n## Usage\n\nEverything after the first newline and before the last will be returned as seen below:\n\n```js\nconst str = multiline(()=\u003e{/*\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n*/});\n```\n\nWhich outputs:\n\n```\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Strip indent\n\nYou can use `multiline.stripIndent()` to be able to indent your multiline string without preserving the redundant leading whitespace.\n\n```js\n\tconst str = multiline.stripIndent(()=\u003e{/*\n\t\t\t\u003c!doctype html\u003e\n\t\t\t\u003chtml\u003e\n\t\t\t\t\u003cbody\u003e\n\t\t\t\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e\n\t\t\t\t\u003c/body\u003e\n\t\t\t\u003c/html\u003e\n\t*/});\n```\n\nWhich outputs:\n\n```\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n```\n\n\n### String substitution\n\n`console.log()` supports [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data):\n\n```js\nconst str = 'unicorns';\n\nconsole.log(multiline(()=\u003e{/*\n  I love %s\n*/}), str);\n\n//=\u003e 'I love unicorns'\n```\n\n\n## Use cases\n\n- [CLI help output](https://github.com/sindresorhus/pageres/blob/cb85922dec2b962c7b45484023c9ba43a9abf6bd/cli.js#L14-L33)\n- [Test fixtures](https://twitter.com/TooTallNate/status/465392558000984064)\n- [Queries](https://github.com/freethejazz/twitter-to-neo4j/blob/a41b6c2e8480d4b9943640a8aa4b6976f07083bf/cypher/queries.js#L15-L22) - *here an example in Cypher, the query language for Neo4j*\n- [CLI welcome message](https://github.com/yeoman/generator-jquery/blob/4b532843663e4b5ce7d433d351e0a78dcf2b1e20/app/index.js#L28-L40) - *here in a Yeoman generator*\n\nHave one? [Let me know.](https://github.com/sindresorhus/multiline/issues/new)\n\n\n## Experiment\n\nI've also done an [experiment](experiment.js) where you don't need the anonymous function. It's too fragile and slow to be practical though.\n\nIt generates a callstack and extracts the contents of the comment in the function call.\n\n```js\nconst str = multiline(/*\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n*/);\n```\n\n\n## FAQ\n\n### But JS already has multiline strings with `\\`?\n\n```js\nconst str = 'foo\\\nbar';\n```\n\nThis is not a multiline string. It's line-continuation. It doesn't preserve newlines, which is the main reason for wanting multiline strings.\n\nYou would need to do the following:\n\n```js\nconst str = 'foo\\n\\\nbar';\n```\n\nBut then you could just as well concatenate:\n\n```js\nconst str = 'foo\\n' +\n'bar';\n```\n\n\n## Browser\n\nWhile it does work fine in the browser, it's mainly intended for use in Node.js. Use at your own risk.\n\n```\n$ npm install multiline\n```\n\nWith Webpack, Browserify, or something similar.\n\n\n### Compatibility\n\n- Latest Chrome\n- Firefox \u003e=17\n- Safari \u003e=4\n- Opera \u003e=9\n- Internet Explorer \u003e=6\n\n### Minification\n\nEven though minifiers strip comments by default there are ways to preserve them:\n\n- Uglify: Use `/*@preserve` instead of `/*` and enable the `comments` option\n- Closure Compiler: Use `/*@preserve` instead of `/*`\n- YUI Compressor: Use `/*!` instead of `/*`\n\nYou also need to add `console.log` after the comment so it's not removed as dead-code.\n\nThe final result would be:\n\n```js\nconst str = multiline(function(){/*!@preserve\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003ch1\u003e❤ unicorns\u003c/h1\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n*/console.log});\n```\n\n\n## License\n\nMIT © [Sindre Sorhus](https://sindresorhus.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fmultiline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fmultiline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fmultiline/lists"}