21de1f2f4b7f8ee373b28d1704721ff759cb28d15add5213c26e99ed514c81a9a7d0988a1cbf143d32d0ab357cf463ab636580d34ef4d905330fa5a082a3bb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # posthtml-rename-id
  2. [PostHTML](https://github.com/posthtml/posthtml) plugin to rename id attributes
  3. and it's references. Inspired by [grunt-svgstore](https://github.com/FWeinb/grunt-svgstore).
  4. Handle following cases:
  5. - `href="#id"` and `xlink:href="#id"`
  6. - `style` attribute values like `style="fill: url(#id)"`
  7. - `<style>` tag values like `.selector {fill: url(#id)"}`
  8. - any other attribute value like `attr="url(#id)"`
  9. ## Demo
  10. Input
  11. ```html
  12. <style>
  13. .selector {fill: url(#qwe)}
  14. </style>
  15. <div id="qwe"></div>
  16. <a href="#qwe"></a>
  17. ```
  18. Output
  19. ```html
  20. <style>
  21. .selector {fill: url(#prefix_qwe)}
  22. </style>
  23. <div id="prefix_qwe"></div>
  24. <a href="#prefix_qwe"></a>
  25. ```
  26. ## Install
  27. ```sh
  28. npm install posthtml-rename-id
  29. ```
  30. ## Usage
  31. ```js
  32. const posthtml = require('posthtml');
  33. const rename = require('posthtml-rename-id');
  34. posthtml()
  35. .use(rename('prefix_[id]'))
  36. .process('<div id="qwe"></div> <a href="#qwe"></a>')
  37. .then(({ html }) => {
  38. console.log(html); // <div id="prefix_qwe"></div> <a href="#prefix_qwe"></a>
  39. });
  40. ```
  41. ## Configuration
  42. ### `pattern`
  43. > Type: `string | function`<br>
  44. > Default: `'[id]'`
  45. Renaming pattern. `[id]` placeholder can be used as current id of an element.
  46. If `pattern` provided as a function it will be called with current id as first argument.
  47. Function should return the new id as string (`[id]` can be used as well).
  48. ## Examples
  49. Uppercase all ids:
  50. ```js
  51. posthtml([
  52. renameId(id => id.toUpperCase())
  53. ]);
  54. ```
  55. Rename all ids to `elem_{counter}`:
  56. ```js
  57. let c = 0;
  58. posthtml([
  59. renameId((id) => { c++; return 'elem_' + c; })
  60. ]);
  61. ```
  62. ## LICENSE
  63. [MIT](https://github.com/JetBrains/svg-mixer/blob/master/LICENSE)