-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtags.ts
More file actions
117 lines (111 loc) · 3.3 KB
/
Copy pathtags.ts
File metadata and controls
117 lines (111 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type { TagOptions, XMLAttributes } from './types';
import { replaceInvalidXmlAttributeCharacters, replaceInvalidXmlCdataCharacters } from './utility';
/**
* Build an XML tag
* @param name - the name of the tag (<name/>)
* @param value - the tag content (optional) (<name>content</name>)
* @param attributes - the tag attributes (optional) (<name att1="att1value"/> or <name att1="att1value">content</name>)
* @param options - the tag options
* @returns the XML tag
*/
export const tag = ({
name,
value,
attributes,
options,
}: {
name: string;
value?: string;
attributes?: XMLAttributes;
options: TagOptions;
}): string =>
value
? tagWithContent({ name, value, attributes, options })
: tagSelfClosing({ name, attributes, options });
/**
* Build a tag with content
* @param name - the name of the tag (<name>content</name>)
* @param value - the tag content
* @param attributes - the tag attributes (optional) (<name att1="att1value">content</name>)
* @param options - the tag options
* @returns the tag with content
*/
export const tagWithContent = ({
name,
value,
attributes,
options: { depth, pretty, indent },
}: {
name: string;
value: string;
attributes?: XMLAttributes;
options: TagOptions;
}): string => {
if (pretty) {
if (value.includes('\n') && !value.startsWith('\n')) value = '\n' + value;
if (value.includes('\n') && !value.endsWith('\n')) value += '\n';
}
if (value.endsWith('\n')) value += ''.padStart(depth * indent);
return (
''.padStart(pretty ? depth * indent : 0) +
`<${name}${tagAttributes(attributes)}>${value}</${name}>${pretty ? '\n' : ''}`
);
};
/**
* Build a self closing XML tag
* @param name - the name of the tag (<name/>)
* @param attributes - the tag attributes (optional) (<name att1="att1value"/>)
* @param options - the tag options
* @returns the self closing tag
*/
export const tagSelfClosing = ({
name,
attributes,
options: { depth, pretty, indent },
}: {
name: string;
attributes?: XMLAttributes;
options: TagOptions;
}): string =>
''.padStart(pretty ? depth * indent : 0) +
`<${name}${tagAttributes(attributes)}/>${pretty ? '\n' : ''}`;
/**
* Build the tag attributes in valid XML format
* @param attributes - the attributes
* @returns the tag attributes as a string
*/
export const tagAttributes = (attributes?: XMLAttributes): string =>
attributes && Object.keys(attributes).length > 0
? ' ' +
Object.entries(attributes)
.map(([key, value]) => `${key}="${replaceInvalidXmlAttributeCharacters(value)}"`)
.join(' ')
: '';
/**
* Build a CDATA tag
* @param value - the content of the CDATA tag
* @returns the CDATA tag
*/
export const cdataTag = ({
value,
options: { depth, pretty, indent },
}: {
value: string;
options: TagOptions;
}): string =>
''.padStart(pretty ? depth * indent : 0) +
`<![CDATA[${replaceInvalidXmlCdataCharacters(value)}]]>${pretty ? '\n' : ''}`;
/**
* Build an XML header tag
* @param name - the name of the tag
* @param attributes - the tag attributes (optional) (<name att1="att1value"/>)
* @param options - the tag options
* @returns the header tag
*/
export const tagXmlHeader = ({
name,
attributes,
}: {
name: string;
attributes?: XMLAttributes;
}): string => `<${name.startsWith('?') ? name : `?${name}`}${tagAttributes(attributes)}?>`;