Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
typescript
json2html
Commits
639fae5b
Commit
639fae5b
authored
Oct 21, 2020
by
Zéfling
🎨
Browse files
Add options 'inline' and 'autoclose' on json node
parent
f6a619d3
Changes
2
Hide whitespace changes
Inline
Side-by-side
projects/json2html/src/lib/json2html.ts
View file @
639fae5b
...
...
@@ -14,7 +14,7 @@ export interface Json2htmlRef {
body
?:
Json2htmlBody
;
/** inline : override formatting option for this element and these children */
inline
?:
boolean
;
/** autoclose
(XML only)
*/
/** autoclose
: ignore body and end tag
*/
autoclose
?:
boolean
;
}
...
...
@@ -139,19 +139,19 @@ export class Json2html {
* @param json node data
* @returns render of node
*/
private
_generate
(
lvl
:
number
,
json
:
Json2htmlRef
):
string
{
private
_generate
(
lvl
:
number
,
json
:
Json2htmlRef
,
inline
:
boolean
=
false
):
string
{
const
hasContent
=
!
this
.
options
.
noContentTags
.
includes
(
json
.
tag
.
toLowerCase
());
const
xmlAutoClose
=
!
hasContent
&&
this
.
options
.
type
===
'
xml
'
?
'
/
'
:
''
;
let
string
=
`<
${
json
.
tag
}${
this
.
_generateAttrs
(
lvl
,
json
)}${
xmlAutoClose
}
>`
;
if
(
hasContent
)
{
let
tagcontent
=
this
.
_generateBody
(
lvl
,
json
);
if
(
tagcontent
&&
this
.
_hasMultiline
())
{
const
xmlAutoClose
=
(
!
hasContent
||
json
.
autoclose
)
&&
this
.
_modeXML
()
?
'
/
'
:
''
;
let
string
=
`<
${
json
.
tag
}${
this
.
_generateAttrs
(
lvl
,
json
,
inline
)}${
xmlAutoClose
}
>`
;
if
(
hasContent
&&
!
json
.
autoclose
)
{
let
tagcontent
=
this
.
_generateBody
(
lvl
,
json
,
inline
||
json
.
inline
);
if
(
tagcontent
&&
this
.
_hasMultiline
()
&&
!
(
inline
||
json
.
inline
)
)
{
tagcontent
=
`
${
tagcontent
}
\n
${
this
.
_getSpacing
(
lvl
)}
`
;
}
string
+=
tagcontent
;
if
(
!
this
.
options
.
removeOptionalEndTags
||
this
.
options
.
type
===
'
xml
'
||
this
.
_modeXML
()
||
this
.
options
.
removeOptionalEndTags
&&
!
this
.
options
.
optionalEndTags
.
includes
(
json
.
tag
.
toLowerCase
())
)
{
string
+=
`</
${
json
.
tag
}
>`
;
...
...
@@ -166,14 +166,14 @@ export class Json2html {
* @param json node data
* @returns attributes tag
*/
private
_generateAttrs
(
lvl
:
number
,
json
:
Json2htmlRef
)
{
private
_generateAttrs
(
lvl
:
number
,
json
:
Json2htmlRef
,
inline
:
boolean
)
{
let
string
=
''
;
const
attrs
=
json
.
attrs
;
if
(
attrs
&&
Object
.
keys
(
attrs
).
length
)
{
for
(
const
id
in
attrs
)
{
if
(
attrs
[
id
]
!==
undefined
)
{
let
attr
=
''
;
switch
(
this
.
options
.
attrPosition
)
{
switch
(
!
inline
?
this
.
options
.
attrPosition
:
'
inline
'
)
{
case
'
inline
'
:
attr
+=
'
'
;
break
;
...
...
@@ -207,14 +207,14 @@ export class Json2html {
* @param json node data
* @returns render of body
*/
private
_generateBody
(
lvl
:
number
,
json
:
Json2htmlRef
)
{
private
_generateBody
(
lvl
:
number
,
json
:
Json2htmlRef
,
inline
:
boolean
)
{
let
string
=
''
;
if
(
json
.
body
)
{
if
(
!
Array
.
isArray
(
json
.
body
))
{
string
+=
this
.
_generateBodyElement
(
lvl
,
json
.
body
,
true
);
string
+=
this
.
_generateBodyElement
(
lvl
,
json
.
body
,
true
,
inline
);
}
else
{
json
.
body
.
forEach
(
element
=>
{
string
+=
this
.
_generateBodyElement
(
lvl
,
element
,
false
);
string
+=
this
.
_generateBodyElement
(
lvl
,
element
,
false
,
inline
);
});
}
}
...
...
@@ -228,14 +228,19 @@ export class Json2html {
* @param onlyOne body this an unique node
* @returns render of body
*/
private
_generateBodyElement
(
lvl
:
number
,
element
:
Json2htmlRef
|
string
,
onlyOne
:
boolean
):
string
{
private
_generateBodyElement
(
lvl
:
number
,
element
:
Json2htmlRef
|
string
,
onlyOne
:
boolean
,
inline
:
boolean
=
false
):
string
{
let
string
=
''
;
if
(
this
.
_hasMultiline
())
{
if
(
this
.
_hasMultiline
()
&&
!
inline
)
{
string
+=
`\n
${
this
.
_getSpacing
(
lvl
+
1
)}
`
;
}
// in XML mode, for generate a valid XML structure
if
(
!
onlyOne
&&
this
.
options
.
type
===
'
xml
'
&&
typeof
element
===
'
string
'
)
{
if
(
!
onlyOne
&&
this
.
_modeXML
()
&&
typeof
element
===
'
string
'
)
{
element
=
{
tag
:
this
.
options
.
xmlDefaultTag
,
body
:
element
...
...
@@ -244,7 +249,7 @@ export class Json2html {
string
+=
typeof
element
===
'
string
'
?
element
:
this
.
_generate
(
lvl
+
1
,
element
);
:
this
.
_generate
(
lvl
+
1
,
element
,
inline
);
return
string
;
}
...
...
@@ -255,6 +260,13 @@ export class Json2html {
return
this
.
options
.
formatting
===
'
multiline
'
;
}
/**
* if multiline rendering
*/
private
_modeXML
():
boolean
{
return
this
.
options
.
type
===
'
xml
'
;
}
/**
* calculating the number of spaces for indentation
* @param lvl level
...
...
src/app/app.json.ts
View file @
639fae5b
export
const
examples
=
[
,
{
tag
:
'
div
'
,
attrs
:
{
id
:
'
test1
'
,
class
:
'
testclasse
'
},
...
...
@@ -40,7 +39,8 @@ export const examples = [
'
test2
'
]
}
]
],
inline
:
false
},
{
tag
:
'
span
'
,
...
...
@@ -49,6 +49,14 @@ export const examples = [
'
test2
'
]
},
{
tag
:
'
test-autoclose
'
,
attrs
:
{
id
:
'
attr-2
'
,
class
:
'
bar
'
},
body
:
[
'
test2
'
],
autoclose
:
true
},
{
tag
:
'
datalist
'
,
attrs
:
{
id
:
'
id-datalist
'
},
...
...
@@ -60,7 +68,8 @@ export const examples = [
{
tag
:
'
option
'
,
attrs
:
{
value
:
'
QRST
'
}
},
{
tag
:
'
option
'
,
attrs
:
{
value
:
'
UVW
'
}
},
{
tag
:
'
option
'
,
attrs
:
{
value
:
'
XYZ
'
}
}
]
],
inline
:
true
}
]
];
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment