@shopify/liquid-html-parser
Advanced tools
Comparing version 2.1.2 to 2.2.0
import ohm from 'ohm-js'; | ||
export declare const liquidHtmlGrammars: ohm.Namespace; | ||
export declare const TextNodeGrammar: ohm.Grammar; | ||
export declare const LiquidDocGrammar: ohm.Grammar; | ||
export interface LiquidGrammars { | ||
@@ -5,0 +6,0 @@ Liquid: ohm.Grammar; |
@@ -6,6 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TAGS_WITHOUT_MARKUP = exports.VOID_ELEMENTS = exports.RAW_TAGS = exports.BLOCKS = exports.placeholderGrammars = exports.tolerantGrammars = exports.strictGrammars = exports.TextNodeGrammar = exports.liquidHtmlGrammars = void 0; | ||
exports.TAGS_WITHOUT_MARKUP = exports.VOID_ELEMENTS = exports.RAW_TAGS = exports.BLOCKS = exports.placeholderGrammars = exports.tolerantGrammars = exports.strictGrammars = exports.LiquidDocGrammar = exports.TextNodeGrammar = exports.liquidHtmlGrammars = void 0; | ||
const ohm_js_1 = __importDefault(require("ohm-js")); | ||
exports.liquidHtmlGrammars = ohm_js_1.default.grammars(require('../grammar/liquid-html.ohm.js')); | ||
exports.TextNodeGrammar = exports.liquidHtmlGrammars['Helpers']; | ||
exports.LiquidDocGrammar = exports.liquidHtmlGrammars['LiquidDoc']; | ||
exports.strictGrammars = { | ||
@@ -43,3 +44,4 @@ Liquid: exports.liquidHtmlGrammars['StrictLiquid'], | ||
'raw', | ||
'doc', | ||
]; | ||
//# sourceMappingURL=grammar.js.map |
@@ -69,3 +69,4 @@ /** | ||
PaginateMarkup = "PaginateMarkup", | ||
RenderVariableExpression = "RenderVariableExpression" | ||
RenderVariableExpression = "RenderVariableExpression", | ||
ContentForNamedArgument = "ContentForNamedArgument" | ||
} | ||
@@ -72,0 +73,0 @@ export declare const LiquidLiteralValues: { |
@@ -76,2 +76,3 @@ "use strict"; | ||
ConcreteNodeTypes["RenderVariableExpression"] = "RenderVariableExpression"; | ||
ConcreteNodeTypes["ContentForNamedArgument"] = "ContentForNamedArgument"; | ||
})(ConcreteNodeTypes = exports.ConcreteNodeTypes || (exports.ConcreteNodeTypes = {})); | ||
@@ -200,2 +201,22 @@ exports.LiquidLiteralValues = { | ||
}, | ||
liquidDoc: { | ||
type: ConcreteNodeTypes.LiquidRawTag, | ||
name: 'doc', | ||
body: (tokens) => tokens[1].sourceString, | ||
children: (tokens) => { | ||
const contentNode = tokens[1]; | ||
return toLiquidDocAST(source, contentNode.sourceString, offset + contentNode.source.startIdx); | ||
}, | ||
whitespaceStart: (tokens) => tokens[0].children[1].sourceString, | ||
whitespaceEnd: (tokens) => tokens[0].children[7].sourceString, | ||
delimiterWhitespaceStart: (tokens) => tokens[2].children[1].sourceString, | ||
delimiterWhitespaceEnd: (tokens) => tokens[2].children[7].sourceString, | ||
locStart, | ||
locEnd, | ||
source, | ||
blockStartLocStart: (tokens) => tokens[0].source.startIdx, | ||
blockStartLocEnd: (tokens) => tokens[0].source.endIdx, | ||
blockEndLocStart: (tokens) => tokens[2].source.startIdx, | ||
blockEndLocEnd: (tokens) => tokens[2].source.endIdx, | ||
}, | ||
liquidInlineComment: { | ||
@@ -427,2 +448,3 @@ type: ConcreteNodeTypes.LiquidTag, | ||
tagArguments: 0, | ||
contentForTagArgument: 0, | ||
positionalArgument: 0, | ||
@@ -437,2 +459,10 @@ namedArgument: { | ||
}, | ||
contentForNamedArgument: { | ||
type: ConcreteNodeTypes.NamedArgument, | ||
name: (node) => node[0].sourceString + node[1].sourceString, | ||
value: 6, | ||
locStart, | ||
locEnd, | ||
source, | ||
}, | ||
liquidString: 0, | ||
@@ -750,2 +780,30 @@ liquidDoubleQuotedString: { | ||
} | ||
/** | ||
* Builds an AST for LiquidDoc content. | ||
* | ||
* `toCST` includes mappings and logic that are not needed for LiquidDoc so we're separating this logic | ||
*/ | ||
function toLiquidDocAST(source, matchingSource, offset) { | ||
// When we switch parser, our locStart and locEnd functions must account | ||
// for the offset of the {% doc %} markup | ||
const locStart = (tokens) => offset + tokens[0].source.startIdx; | ||
const locEnd = (tokens) => offset + tokens[tokens.length - 1].source.endIdx; | ||
const res = grammar_1.LiquidDocGrammar.match(matchingSource, 'Node'); | ||
if (res.failed()) { | ||
throw new errors_1.LiquidHTMLCSTParsingError(res); | ||
} | ||
const LiquidDocMappings = { | ||
Node: 0, | ||
TextNode: { | ||
type: ConcreteNodeTypes.TextNode, | ||
value: function () { | ||
return this.sourceString; | ||
}, | ||
locStart, | ||
locEnd, | ||
source, | ||
}, | ||
}; | ||
return (0, extras_1.toAST)(res, LiquidDocMappings); | ||
} | ||
//# sourceMappingURL=stage-1-cst.js.map |
@@ -40,2 +40,3 @@ module.exports = String.raw`Helpers { | ||
liquidNode = | ||
| liquidDoc | ||
| liquidBlockComment | ||
@@ -131,4 +132,9 @@ | liquidRawTag | ||
liquidTagContentFor = liquidTagRule<"content_for", liquidTagContentForMarkup> | ||
liquidTagContentForMarkup = | ||
contentForType (argumentSeparatorOptionalComma tagArguments) (space* ",")? space* | ||
contentForType (argumentSeparatorOptionalComma contentForTagArgument) (space* ",")? space* | ||
contentForTagArgument = listOf<contentForNamedArgument<delimTag>, argumentSeparatorOptionalComma> | ||
contentForNamedArgument<delim> = (variableSegment ("." variableSegment)*) space* ":" space* (liquidExpression<delim>) | ||
contentForType = liquidString<delimTag> | ||
@@ -216,2 +222,11 @@ | ||
liquidDoc = | ||
liquidDocStart | ||
liquidDocBody | ||
liquidDocEnd | ||
liquidDocStart = "{%" "-"? space* ("doc" endOfIdentifier) space* tagMarkup "-"? "%}" | ||
liquidDocEnd = "{%" "-"? space* ("enddoc" endOfIdentifier) space* tagMarkup "-"? "%}" | ||
liquidDocBody = anyExceptStar<(liquidDocStart | liquidDocEnd)> | ||
// In order for the grammar to "fallback" to the base case, this | ||
@@ -379,2 +394,6 @@ // rule must pass if and only if we support what we parse. This | ||
LiquidDoc <: Helpers { | ||
Node := (TextNode)* | ||
} | ||
LiquidHTML <: Liquid { | ||
@@ -381,0 +400,0 @@ Node := yamlFrontmatter? (HtmlNode | liquidNode | TextNode)* |
{ | ||
"name": "@shopify/liquid-html-parser", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Liquid HTML parser by Shopify", | ||
@@ -5,0 +5,0 @@ "author": "CP Clermont <cp.clermont@shopify.com>", |
Sorry, the diff of this file is not supported yet
162999
3672