Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dynamic include feature #3413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion packages/pug-lexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,46 @@ Lexer.prototype = {
*/

include: function() {
var tok = this.scan(/^include(?=:| |$|\n)/, 'include');
var tok = this.scan(/^include(?=:| |=|$|\n)/, 'include');
if (tok) {
this.tokens.push(this.tokEnd(tok));
while (this.callLexerFunction('filter', {inInclude: true}));

/*
* to support the `include=dynamicPath`
* dynamicPath could contains interpolation like #{someVariables}
*/
if(/^=/.exec(this.input)){
const pathString = this.input.split('=');
const path = pathString[1].trim();
// to ensure no impact the exisitng include which will accept the param after the whitepsace
this.input = ' ' + path.replaceAll(/['"`]/g, "");

const variablesInPath = this.input.match(/#{([^\/]+)}/g) || [];

variablesInPath.forEach(variableName => {
const originalVariableName = variableName;
variableName = variableName.slice(2, -1);
for(let token of this.tokens){
if(token.type === 'code'){
const regex = /(var|let|const)\s+(\w+)\s*=\s*(?:"([^"]+)"|`([^`]+)`|'([^']+)')/;
const match = token.val.match(regex);

if(match && match[2] === variableName) {
const splittedValues = token.val.split('=');
let rightSideValue = splittedValues[1];
rightSideValue = rightSideValue.replaceAll(/['";`]/g, "").trim();

if(rightSideValue){
this.input = this.input.replace(originalVariableName,rightSideValue);
break;
}
}
}
}
})
}

if (!this.callLexerFunction('path')) {
if (/^[^ \n]+/.test(this.input)) {
// if there is more text
Expand Down
15 changes: 15 additions & 0 deletions packages/pug/test/pug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,21 @@ describe('pug', function() {
);
});

it('should include block', function() {
var str = [
'html',
' head',
' - const includeFilePath = "fixtures/scripts.pug"',
' include=#{includeFilePath}',
' scripts(src="/app.js")',
].join('\n');

assert.equal(
'<html><head><script src="/jquery.js"></script><script src="/caustic.js"></script><scripts src="/app.js"></scripts></head></html>',
pug.render(str, {filename: __dirname + '/pug.test.js'})
);
});

it('should not fail on js newlines', function() {
assert.equal('<p>foo\u2028bar</p>', pug.render('p foo\u2028bar'));
assert.equal('<p>foo\u2029bar</p>', pug.render('p foo\u2029bar'));
Expand Down