File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { defineBuildConfig } from 'unbuild'
2+
3+ export default defineBuildConfig ( {
4+ rollup : {
5+ emitCJS : true ,
6+ } ,
7+ rootDir : './lib' ,
8+ outDir : '../dist' ,
9+ entries : [ 'index.js' ] ,
10+ declaration : true ,
11+ } )
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const omit = require ( 'lodash.omit' ) ;
4+ const matchHelper = require ( 'posthtml-match-helper' ) ;
5+
6+ function _interopDefaultCompat ( e ) { return e && typeof e === 'object' && 'default' in e ? e . default : e ; }
7+
8+ const omit__default = /*#__PURE__*/ _interopDefaultCompat ( omit ) ;
9+ const matchHelper__default = /*#__PURE__*/ _interopDefaultCompat ( matchHelper ) ;
10+
11+ const plugin = ( options = { } ) => ( tree ) => {
12+ options . attributes = options . attributes || { } ;
13+ options . overwrite = options . overwrite || false ;
14+ const process = ( node ) => {
15+ const attributes = Object . keys ( options . attributes ) ;
16+ const matchers = attributes . map ( ( attribute ) => matchHelper__default ( attribute ) ) ;
17+ attributes . forEach ( ( key , i ) => {
18+ tree . match ( matchers [ i ] , ( node2 ) => {
19+ for ( const [ k , v ] of Object . entries ( options . attributes [ key ] ) ) {
20+ if ( k === "class" && node2 . attrs && node2 . attrs . class ) {
21+ node2 . attrs . class = [ .../* @__PURE__ */ new Set ( [ ...node2 . attrs . class . split ( " " ) , ...v . split ( " " ) ] ) ] . join ( " " ) ;
22+ } else {
23+ const attributes2 = options . overwrite ? options . attributes [ key ] : omit__default ( options . attributes [ key ] , Object . keys ( node2 . attrs || { } ) ) ;
24+ node2 . attrs = { ...node2 . attrs , ...attributes2 } ;
25+ }
26+ }
27+ return node2 ;
28+ } ) ;
29+ } ) ;
30+ return node ;
31+ } ;
32+ return tree . walk ( process ) ;
33+ } ;
34+
35+ module . exports = plugin ;
Original file line number Diff line number Diff line change 1+ type PluginOptions = {
2+ /**
3+ An object defining which elements should get what attributes.
4+
5+ @default {}
6+
7+ @example
8+ {
9+ table: {
10+ cellpadding: 0,
11+ cellspacing: 0,
12+ role: 'none'
13+ },
14+ img: {
15+ alt: ''
16+ },
17+ }
18+ */
19+ attributes ?: {
20+ [ tagName : string ] : {
21+ [ attribute : string ] : string | number ;
22+ } ;
23+ } ;
24+
25+ /**
26+ * Overwrite existing attribute values.
27+ *
28+ * @default false
29+ *
30+ * @example
31+ * {
32+ * overwrite: true
33+ * }
34+ */
35+ overwrite ?: boolean ;
36+ } ;
37+
38+ export type { PluginOptions } ;
Original file line number Diff line number Diff line change 1+ type PluginOptions = {
2+ /**
3+ An object defining which elements should get what attributes.
4+
5+ @default {}
6+
7+ @example
8+ {
9+ table: {
10+ cellpadding: 0,
11+ cellspacing: 0,
12+ role: 'none'
13+ },
14+ img: {
15+ alt: ''
16+ },
17+ }
18+ */
19+ attributes ?: {
20+ [ tagName : string ] : {
21+ [ attribute : string ] : string | number ;
22+ } ;
23+ } ;
24+
25+ /**
26+ * Overwrite existing attribute values.
27+ *
28+ * @default false
29+ *
30+ * @example
31+ * {
32+ * overwrite: true
33+ * }
34+ */
35+ overwrite ?: boolean ;
36+ } ;
37+
38+ export type { PluginOptions } ;
Original file line number Diff line number Diff line change 1+ type PluginOptions = {
2+ /**
3+ An object defining which elements should get what attributes.
4+
5+ @default {}
6+
7+ @example
8+ {
9+ table: {
10+ cellpadding: 0,
11+ cellspacing: 0,
12+ role: 'none'
13+ },
14+ img: {
15+ alt: ''
16+ },
17+ }
18+ */
19+ attributes ?: {
20+ [ tagName : string ] : {
21+ [ attribute : string ] : string | number ;
22+ } ;
23+ } ;
24+
25+ /**
26+ * Overwrite existing attribute values.
27+ *
28+ * @default false
29+ *
30+ * @example
31+ * {
32+ * overwrite: true
33+ * }
34+ */
35+ overwrite ?: boolean ;
36+ } ;
37+
38+ export type { PluginOptions } ;
Original file line number Diff line number Diff line change 1+ import omit from 'lodash.omit' ;
2+ import matchHelper from 'posthtml-match-helper' ;
3+
4+ const plugin = ( options = { } ) => ( tree ) => {
5+ options . attributes = options . attributes || { } ;
6+ options . overwrite = options . overwrite || false ;
7+ const process = ( node ) => {
8+ const attributes = Object . keys ( options . attributes ) ;
9+ const matchers = attributes . map ( ( attribute ) => matchHelper ( attribute ) ) ;
10+ attributes . forEach ( ( key , i ) => {
11+ tree . match ( matchers [ i ] , ( node2 ) => {
12+ for ( const [ k , v ] of Object . entries ( options . attributes [ key ] ) ) {
13+ if ( k === "class" && node2 . attrs && node2 . attrs . class ) {
14+ node2 . attrs . class = [ .../* @__PURE__ */ new Set ( [ ...node2 . attrs . class . split ( " " ) , ...v . split ( " " ) ] ) ] . join ( " " ) ;
15+ } else {
16+ const attributes2 = options . overwrite ? options . attributes [ key ] : omit ( options . attributes [ key ] , Object . keys ( node2 . attrs || { } ) ) ;
17+ node2 . attrs = { ...node2 . attrs , ...attributes2 } ;
18+ }
19+ }
20+ return node2 ;
21+ } ) ;
22+ } ) ;
23+ return node ;
24+ } ;
25+ return tree . walk ( process ) ;
26+ } ;
27+
28+ export { plugin as default } ;
You can’t perform that action at this time.
0 commit comments