-
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.
attributesTransformerを追加し、要素に属性を追加する機能を実装
- Loading branch information
Showing
7 changed files
with
106 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import attributesTransformer from "../../src/transformer/attributes"; | ||
import { cheerioLoad } from "../test-utils"; | ||
|
||
describe("attributesTransformer", () => { | ||
test("正常系: 要素に属性が追加されること", async () => { | ||
const $ = cheerioLoad(` | ||
<div id="id"></div> | ||
`); | ||
|
||
await attributesTransformer({ | ||
elements: { | ||
div: { | ||
addAttributes: { | ||
class: "test-class", | ||
"data-test": "test-value", | ||
id: ($element) => { | ||
const originalId = $element.attr("id"); | ||
return `${originalId}-modified`; | ||
}, | ||
}, | ||
}, | ||
}, | ||
})($); | ||
|
||
expect($.html()).toBe(` | ||
<div id="id-modified" class="test-class" data-test="test-value"></div> | ||
`); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import type { CheerioAPI } from "cheerio"; | ||
|
||
export type Extractor<T> = ($: CheerioAPI) => Promise<T>; | ||
export type Extractor<T> = ($: CheerioAPI) => Promise<T> | T; |
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 @@ | ||
import type { Cheerio, SelectorType } from "cheerio"; | ||
import type { Element } from "domhandler"; | ||
import type { Transformer } from "./types"; | ||
|
||
type Options = { | ||
/** | ||
* 処理したい要素の一覧 | ||
*/ | ||
elements: { | ||
/** | ||
* 要素名 | ||
*/ | ||
[tagName: string]: { | ||
/** | ||
* 追加した属性名と値の一覧 | ||
* 単純文字列のほかに対象の要素を引数に取る関数を指定することもできる | ||
*/ | ||
addAttributes: { | ||
[attributeName: string]: | ||
| string | ||
| (($element: Cheerio<Element>) => string); | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
/** | ||
* 任意の要素に対して属性を追加したり変更したりする | ||
*/ | ||
const attributesTransformer: (options: Options) => Transformer = | ||
(options) => ($) => { | ||
for (const [_tagName, { addAttributes }] of Object.entries( | ||
options.elements, | ||
)) { | ||
const tagName = _tagName as SelectorType; | ||
|
||
$(tagName).each((_, element) => { | ||
const $element = $(element); | ||
for (const [attributeName, attributeValue] of Object.entries( | ||
addAttributes, | ||
)) { | ||
if (typeof attributeValue === "string") { | ||
$element.attr(attributeName, attributeValue); | ||
} else { | ||
$element.attr(attributeName, attributeValue($element)); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
export default attributesTransformer; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import type { CheerioAPI } from "cheerio"; | ||
|
||
export type Transformer = ($: CheerioAPI) => Promise<void>; | ||
export type Transformer = ($: CheerioAPI) => Promise<void> | void; |