diff --git a/src/jinja.ts b/src/jinja.ts index e218dcc..5a99021 100644 --- a/src/jinja.ts +++ b/src/jinja.ts @@ -7,6 +7,7 @@ export interface Node { id: string; type: "root" | "expression" | "statement" | "block" | "ignore"; content: string; + ownLine: boolean; originalText: string; index: number; length: number; @@ -15,7 +16,6 @@ export interface Node { export interface Expression extends Node { type: "expression"; - ownLine: boolean; } export type Delimiter = "" | "-" | "+"; @@ -35,7 +35,6 @@ export interface Block extends Node { export interface IgnoreBlock extends Node { type: "ignore"; - ownLine: boolean; } export type Keyword = diff --git a/src/parser.ts b/src/parser.ts index 6d840e6..168d28d 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -21,6 +21,7 @@ export const parse: Parser["parse"] = (text) => { id: "0", type: "root" as const, content: text, + ownLine: false, originalText: text, index: 0, length: 0, @@ -101,6 +102,7 @@ export const parse: Parser["parse"] = (text) => { id: placeholder, type: "statement", content: statement, + ownLine: newline, originalText: matchText, index: match.index + i + pre.length, length: matchText.length, @@ -116,6 +118,7 @@ export const parse: Parser["parse"] = (text) => { id: generatePlaceholder(), type: "statement" as const, content: statement, + ownLine: newline, originalText: matchText, index: match.index + i + pre.length, length: matchText.length, @@ -154,6 +157,7 @@ export const parse: Parser["parse"] = (text) => { id: generatePlaceholder(), type: "statement" as const, content: statement, + ownLine: newline, originalText: matchText, index: match.index + i + pre.length, length: matchText.length, @@ -175,6 +179,7 @@ export const parse: Parser["parse"] = (text) => { start: start, end: end, content, + ownLine: newline, originalText: matchText, index: start.index, length: end.index + end.length - start.index, diff --git a/src/printer.ts b/src/printer.ts index 9ff35ae..c9ed579 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -47,7 +47,7 @@ const printStatement = (node: Statement): builders.Doc => { node.endDelimiter, "%}", ], - { shouldBreak: true } + { shouldBreak: node.ownLine } ); return node.keyword === "else" @@ -130,10 +130,20 @@ export const embed: Printer["embed"] = ( }); if (node.type === "block") { + if (node.content.includes("\n")) { + return builders.group([ + path.call(print, "nodes", (node as Block).start.id), + builders.indent([ + builders.softline, + utils.stripTrailingHardline(mapped), + ]), + builders.hardline, + path.call(print, "nodes", (node as Block).end.id), + ]); + } return builders.group([ path.call(print, "nodes", (node as Block).start.id), - builders.indent([builders.softline, utils.stripTrailingHardline(mapped)]), - builders.hardline, + utils.stripTrailingHardline(mapped), path.call(print, "nodes", (node as Block).end.id), ]); } diff --git a/test/cases/statement_inline/expected.html b/test/cases/statement_inline/expected.html new file mode 100644 index 0000000..3295289 --- /dev/null +++ b/test/cases/statement_inline/expected.html @@ -0,0 +1,3 @@ +{% block title %}{% endblock %} + +
class="{% if class %}{{ class }}{% endif %}"
diff --git a/test/cases/statement_inline/input.html b/test/cases/statement_inline/input.html new file mode 100644 index 0000000..63d25b4 --- /dev/null +++ b/test/cases/statement_inline/input.html @@ -0,0 +1,3 @@ +{%block title%} {%endblock%} + +
class="{%if class%}{{class}}{%endif%}"
\ No newline at end of file