├── .editorconfig ├── .github └── workflows │ ├── ci.yml │ └── esm-lint.yml ├── .gitignore ├── .npmrc ├── esm └── package.json ├── index.ts ├── license ├── package.json ├── readme.md ├── test.mjs └── tsconfig.json /.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 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | - pull_request 5 | - push 6 | 7 | jobs: 8 | Lint: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v4 12 | - uses: actions/setup-node@v4 13 | with: 14 | node-version-file: package.json 15 | - run: npm install 16 | - name: XO 17 | run: npx xo 18 | 19 | Test: 20 | runs-on: ubuntu-latest 21 | steps: 22 | - uses: actions/checkout@v4 23 | - uses: actions/setup-node@v4 24 | with: 25 | node-version-file: package.json 26 | - run: npm install 27 | - run: npm run build 28 | - run: node --test 29 | 30 | Build: 31 | runs-on: ubuntu-latest 32 | steps: 33 | - uses: actions/checkout@v4 34 | - uses: actions/setup-node@v4 35 | with: 36 | node-version-file: package.json 37 | - run: npm install 38 | - run: npm run build 39 | 40 | Spelling: 41 | runs-on: ubuntu-latest 42 | steps: 43 | - uses: actions/checkout@v4 44 | - name: Avoid common misspelling 45 | run: "! git grep -E 'code-tags|code tag' -- :^.github" 46 | -------------------------------------------------------------------------------- /.github/workflows/esm-lint.yml: -------------------------------------------------------------------------------- 1 | env: 2 | IMPORT_TEXT: import {html, css, gql, md} from 3 | NPM_MODULE_NAME: code-tag 4 | NODE_VERSION: 18 5 | 6 | # FILE GENERATED WITH: npx ghat fregante/ghatemplates/esm-lint 7 | # SOURCE: https://github.com/fregante/ghatemplates 8 | 9 | name: ESM 10 | on: 11 | pull_request: 12 | branches: 13 | - '*' 14 | push: 15 | branches: 16 | - master 17 | - main 18 | jobs: 19 | Pack: 20 | runs-on: ubuntu-latest 21 | steps: 22 | - uses: actions/checkout@v4 23 | - run: npm install 24 | - run: npm run build --if-present 25 | - run: npm pack --dry-run 26 | - run: npm pack | tail -1 | xargs -n1 tar -xzf 27 | - uses: actions/upload-artifact@v3 28 | with: 29 | path: package 30 | Webpack: 31 | runs-on: ubuntu-latest 32 | needs: Pack 33 | steps: 34 | - uses: actions/download-artifact@v3 35 | - run: npm install ./artifact 36 | - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js 37 | - run: webpack --entry ./index.js 38 | - run: cat dist/main.js 39 | Parcel: 40 | runs-on: ubuntu-latest 41 | needs: Pack 42 | steps: 43 | - uses: actions/download-artifact@v3 44 | - run: npm install ./artifact 45 | - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js 46 | - run: npx parcel@2 build index.js 47 | - run: cat dist/index.js 48 | Rollup: 49 | runs-on: ubuntu-latest 50 | needs: Pack 51 | steps: 52 | - uses: actions/download-artifact@v3 53 | - run: npm install ./artifact rollup@2 @rollup/plugin-node-resolve 54 | - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js 55 | - run: npx rollup -p node-resolve index.js 56 | Vite: 57 | runs-on: ubuntu-latest 58 | needs: Pack 59 | steps: 60 | - uses: actions/download-artifact@v3 61 | - run: npm install ./artifact 62 | - run: >- 63 | echo '' > index.html 65 | - run: npx vite build 66 | - run: cat dist/assets/* 67 | esbuild: 68 | runs-on: ubuntu-latest 69 | needs: Pack 70 | steps: 71 | - uses: actions/download-artifact@v3 72 | - run: echo '{}' > package.json 73 | - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js 74 | - run: npm install ./artifact 75 | - run: npx esbuild --bundle index.js 76 | TypeScript: 77 | runs-on: ubuntu-latest 78 | needs: Pack 79 | steps: 80 | - uses: actions/download-artifact@v3 81 | - run: npm install ./artifact 82 | - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.ts 83 | - run: tsc index.ts 84 | - run: cat index.js 85 | Node: 86 | runs-on: ubuntu-latest 87 | needs: Pack 88 | steps: 89 | - uses: actions/download-artifact@v3 90 | - uses: actions/setup-node@v4 91 | with: 92 | node-version: ${{ env.NODE_VERSION }} 93 | - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.mjs 94 | - run: npm install ./artifact 95 | - run: node index.mjs 96 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | Desktop.ini 4 | ._* 5 | Thumbs.db 6 | *.tmp 7 | *.bak 8 | *.log 9 | logs 10 | *.map 11 | index.js 12 | index.d.ts 13 | !esm/package.json 14 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /esm/package.json: -------------------------------------------------------------------------------- 1 | {"type": "module"} 2 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | // Prettier supports these languages: 2 | // https://github.com/prettier/prettier/blob/e46aba0ab279c764dc26e0f41f15c55122440c51/src/language-js/embed.js#L13 3 | 4 | const concatenateTemplateLiteralTag = ( 5 | raw: TemplateStringsArray, 6 | ...keys: string[] 7 | ): string => keys.length === 0 ? raw[0]! : String.raw({raw}, ...keys); 8 | 9 | /** 10 | Enable highlighting/prettifying when used as html`
This is inline HTML
13 | 14 | 15 | `; 16 | ``` 17 | 18 | You can find such tag functions in: 19 | 20 | - **code-tag**: this package, it returns the string as is 21 | - [escape-goat](https://github.com/sindresorhus/escape-goat): it escapes the any replaced value in the string 22 | - [lit-html](https://lit.dev/docs/templates/overview/): it helps write Web Components 23 | - [Apollo](https://www.apollographql.com/docs/resources/graphql-glossary/#gql-function): it parses GraphQL strings 24 | - [Emotion](https://emotion.sh/docs/@emotion/css): it defines CSS-in-JS 25 | - etc… 26 | 27 | Here are some tools that support them natively: 28 | 29 | - [Prettier](https://prettier.io/docs/en/options.html#embedded-language-formatting): it formats the strings as real non-JavaScript code 30 | - GitHub: it highlights the syntax in the strings as code (as seen in the example above) 31 | 32 | ## Install 33 | 34 | ```sh 35 | npm install code-tag 36 | ``` 37 | 38 | ## Usage 39 | 40 | ```js 41 | import {html, css, gql, md, sql} from 'code-tag'; 42 | // Or: 43 | // const {html, css, gql, md, sql} = require('code-tag'); 44 | 45 | document.body.innerHTML = html` 46 |This is HTML in JS
47 | `; 48 | 49 | document.querySelector('style').textContent = css` 50 | .this.is { 51 | css: 'in JS'; 52 | } 53 | `; 54 | 55 | await githubQuery(gql` 56 | query { 57 | repository(owner: "fregante", name: "template-tags") { 58 | nameWithOwner 59 | } 60 | } 61 | `); 62 | 63 | yourMarkdownConverter(md` 64 | # Markdown 65 | 66 | Is _highlighted_ [as well](https://www.youtube.com/watch?v=dQw4w9WgXcQ) 67 | `); 68 | 69 | await sqlQuery(sql`select * from users`); 70 | ``` 71 | 72 | There's also an `any` export that you can rename as you please: 73 | 74 | ```js 75 | import {any as mdx} from 'code-tag'; 76 | 77 | mdx` 78 | Some other