alpinejs-ssr
Advanced tools
Comparing version
132
index.js
@@ -1,77 +0,77 @@ | ||
import cheerio from "cheerio"; | ||
import _ from "lodash"; | ||
import * as cheerio from 'cheerio'; | ||
import _ from 'lodash'; | ||
function compile(html, data) { | ||
const $ = cheerio.load(html); | ||
const $ = cheerio.load(html); | ||
$("[x-for]").each(function () { | ||
const key = $(this).attr("x-for").split(" in ")[1].slice(5); // Remove 'data.' from the start | ||
const items = _.get(data, key); | ||
const template = $(this).html(); | ||
$('[x-for]').each(function () { | ||
const key = $(this).attr('x-for').split(' in ')[1].slice(5); // Remove 'data.' from the start | ||
const items = _.get(data, key); | ||
const template = $(this).html(); | ||
let result = ""; | ||
items.forEach((item) => { | ||
let itemHtml = cheerio.load(template); | ||
for (const prop in item) { | ||
const regex = new RegExp(`item.${prop}`, "g"); | ||
itemHtml("[x-html]").each(function () { | ||
if ($(this).attr("x-html") === `item.${prop}`) { | ||
$(this).html(item[prop]); | ||
} | ||
let result = ''; | ||
items.forEach((item) => { | ||
let itemHtml = cheerio.load(template); | ||
for (const prop in item) { | ||
const regex = new RegExp(`item.${prop}`, 'g'); | ||
itemHtml('[x-html]').each(function () { | ||
if ($(this).attr('x-html') === `item.${prop}`) { | ||
$(this).html(item[prop]); | ||
} | ||
}); | ||
itemHtml('[x-text]').each(function () { | ||
if ($(this).attr('x-text') === `item.${prop}`) { | ||
$(this).text(item[prop]); | ||
} | ||
}); | ||
itemHtml('[:src]').each(function () { | ||
if ($(this).attr(':src') === `item.${prop}`) { | ||
$(this).attr('src', item[prop]); | ||
} | ||
}); | ||
itemHtml('[:id]').each(function () { | ||
if ($(this).attr(':id') === `item.${prop}`) { | ||
$(this).attr('id', item[prop]); | ||
} | ||
}); | ||
} | ||
result += itemHtml.html(); | ||
}); | ||
itemHtml("[x-text]").each(function () { | ||
if ($(this).attr("x-text") === `item.${prop}`) { | ||
$(this).text(item[prop]); | ||
} | ||
}); | ||
itemHtml("[:src]").each(function () { | ||
if ($(this).attr(":src") === `item.${prop}`) { | ||
$(this).attr("src", item[prop]); | ||
} | ||
}); | ||
itemHtml("[:id]").each(function () { | ||
if ($(this).attr(":id") === `item.${prop}`) { | ||
$(this).attr("id", item[prop]); | ||
} | ||
}); | ||
} | ||
result += itemHtml.html(); | ||
$(this).replaceWith(result); | ||
}); | ||
$(this).replaceWith(result); | ||
}); | ||
$('[x-html]').each(function () { | ||
const key = $(this).attr('x-html').slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).html(value); | ||
} | ||
}); | ||
$("[x-html]").each(function () { | ||
const key = $(this).attr("x-html").slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).html(value); | ||
} | ||
}); | ||
$('[x-text]').each(function () { | ||
const key = $(this).attr('x-text').slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).text(value); | ||
} | ||
}); | ||
$("[x-text]").each(function () { | ||
const key = $(this).attr("x-text").slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).text(value); | ||
} | ||
}); | ||
$('[:src]').each(function () { | ||
const key = $(this).attr(':src').slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).attr('src', value); | ||
} | ||
}); | ||
$("[:src]").each(function () { | ||
const key = $(this).attr(":src").slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).attr("src", value); | ||
} | ||
}); | ||
$('[:id]').each(function () { | ||
const key = $(this).attr(':id').slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).attr('id', value); | ||
} | ||
}); | ||
$("[:id]").each(function () { | ||
const key = $(this).attr(":id").slice(5); // Remove 'data.' from the start | ||
const value = _.get(data, key); | ||
if (value) { | ||
$(this).attr("id", value); | ||
} | ||
}); | ||
/* | ||
/* | ||
$('[x-if]').each(function() { | ||
@@ -86,5 +86,5 @@ const condition = $(this).attr('x-if').slice(5); // Remove 'data.' from the start | ||
return $.html(); | ||
return $.html(); | ||
} | ||
export { compile }; |
{ | ||
"name": "alpinejs-ssr", | ||
"version": "1.0.1", | ||
"description": "Dead simple server-side-rendering for Alpine.js.", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Dashpilot", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dashpilot/alpinejs-ssr" | ||
}, | ||
"license": "ISC", | ||
"dependencies": { | ||
"cheerio": "^1.0.0-rc.12", | ||
"lodash": "^4.17.21" | ||
} | ||
"name": "alpinejs-ssr", | ||
"version": "1.0.2", | ||
"description": "Dead simple server-side-rendering for Alpine.js.", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Dashpilot", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dashpilot/alpinejs-ssr" | ||
}, | ||
"license": "ISC", | ||
"dependencies": { | ||
"cheerio": "^1.0.0", | ||
"lodash": "^4.17.21" | ||
} | ||
} |
6606
6.63%Updated