-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nova funcionalidade, create picture and add sources
- Loading branch information
1 parent
a773e9a
commit 189369b
Showing
15 changed files
with
491 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getTagsImage = getTagsImage; | ||
const fs_1 = require("fs"); | ||
const cheerio = __importStar(require("cheerio")); | ||
function getTagsImage(file) { | ||
const $ = cheerio.load((0, fs_1.readFileSync)(file, "utf8")); | ||
$("img").each((index, img) => { | ||
const filenameUntyped = img.attribs.src.split(".")[0]; | ||
$(img).replaceWith(` | ||
<picture> | ||
<source | ||
srcset="${filenameUntyped + '.avif'}" | ||
type="image/avif" | ||
/> | ||
<source | ||
srcset="${filenameUntyped + '.webp'}" | ||
type="image/webp" | ||
/> | ||
<img | ||
src="${filenameUntyped + '.png'}" | ||
width="${img.attribs.width}" | ||
height="${img.attribs.height}" | ||
alt="${img.attribs.alt}" | ||
/> | ||
</picture> | ||
`); | ||
}); | ||
(0, fs_1.writeFileSync)(file, $.html()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import { typeMarking, verifyTypeFile } from "../files"; | ||
import * as cheerio from "cheerio"; | ||
|
||
export function getTagsImage(file:string) { | ||
const $ = cheerio.load(readFileSync(file, "utf8")); | ||
$("img").each((index, img) => { | ||
const filenameUntyped = img.attribs.src.split(".")[0]; | ||
|
||
$(img).replaceWith(` | ||
<picture> | ||
<source | ||
srcset="${filenameUntyped+'.avif'}" | ||
type="image/avif" | ||
/> | ||
<source | ||
srcset="${filenameUntyped+'.webp'}" | ||
type="image/webp" | ||
/> | ||
<img | ||
src="${filenameUntyped+'.png'}" | ||
width="${img.attribs.width}" | ||
height="${img.attribs.height}" | ||
alt="${img.attribs.alt}" | ||
/> | ||
</picture> | ||
`) | ||
}) | ||
|
||
writeFileSync(file, $.html()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const node_fs_1 = require("node:fs"); | ||
const node_path_1 = __importDefault(require("node:path")); | ||
const node_process_1 = require("node:process"); | ||
const cheerio = __importStar(require("cheerio")); | ||
const _1 = require("."); | ||
describe("Sources", () => { | ||
const testDir = node_path_1.default.join((0, node_process_1.cwd)(), "/tests/files/", "/out"); | ||
const tmpDir = node_path_1.default.join((0, node_process_1.cwd)(), "/tests/files/", "/tmp/sources"); | ||
beforeEach(() => { | ||
(0, node_fs_1.cpSync)(testDir, tmpDir, { recursive: true }); | ||
}); | ||
afterEach(() => { | ||
(0, node_fs_1.rmSync)(tmpDir, { recursive: true }); | ||
}); | ||
it("Deve ter buscado os arquivos e adicionado os pictures", () => { | ||
const file = node_path_1.default.join(tmpDir, "index.html"); | ||
const $old = cheerio.loadBuffer((0, node_fs_1.readFileSync)(file)); | ||
(0, _1.getTagsImage)(file); | ||
const $new = cheerio.loadBuffer((0, node_fs_1.readFileSync)(file)); | ||
expect($old("img").get().length).toBe($new("picture").get().length); | ||
expect($old("img").get().length).toBe($new("img").get().length); | ||
expect($old("img").get().length).toBe($new("source").get().length / 2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { cpSync, readFileSync, rmSync } from "node:fs"; | ||
import path from "node:path"; | ||
import { cwd } from "node:process"; | ||
import * as cheerio from "cheerio"; | ||
import { getTagsImage } from "."; | ||
|
||
describe("Sources", () => { | ||
const testDir = path.join(cwd(), "/tests/files/", "/out"); | ||
const tmpDir = path.join(cwd(), "/tests/files/", "/tmp/sources"); | ||
|
||
beforeEach(() => { | ||
cpSync(testDir, tmpDir, { recursive: true }); | ||
}); | ||
|
||
afterEach(() => { | ||
rmSync(tmpDir, { recursive: true }); | ||
}) | ||
|
||
it("Deve ter buscado os arquivos e adicionado os pictures", () => { | ||
const file = path.join(tmpDir, "index.html"); | ||
const $old = cheerio.loadBuffer(readFileSync(file)); | ||
getTagsImage(file); | ||
const $new = cheerio.loadBuffer(readFileSync(file)); | ||
expect($old("img").get().length).toBe($new("picture").get().length) | ||
expect($old("img").get().length).toBe($new("img").get().length) | ||
expect($old("img").get().length).toBe($new("source").get().length / 2) | ||
}); | ||
}) |
Oops, something went wrong.