Home Reference Source
import ArticleImage from 'wearticle/src/class/ArticleImage.js'
public class | since 0.4.0 | source

ArticleImage

A class for images in WeChat articles.

You'll find this in the returned array of WeChatArticle#getMedia. You can use instanceof to find out if that element is an image.

This library itself doesn't handle images, since many problems are closely related to your implementations. But we do provide a set of API for easier image handling. You can get the URL, remove it out of the article and set a new URL to where you caches all images. Additionally you can directly stream the image, and clean up that <img> for cleaner markup.

Constructor Summary

Public Constructor
public

constructor($img: $())

Takes a cheerio object($(someSelector)).

Member Summary

Public Members
public
public
public

size: *

public
public
public
Private Members
private

_$img: *

private
private

Method Summary

Public Methods
public

Cleans up the <img> tag.

public

get$Img(): *

Get the underlying $(img).

public

isRemoved(): *

See if you've already removed it.

public

remove()

Removes current image from the HTML.

public

setUrl(newUrl: *)

Set src for the picture.

public

async stream(): Stream

Returns a stream of that iamges.

Public Constructors

public constructor($img: $()) source

Takes a cheerio object($(someSelector)). You won't new one in most cases. You can get all meta information of this image immediately.

Params:

NameTypeAttributeDescription
$img $()

a cheerio object representing an <img> in original WeChat article.

Public Members

public ratio: number source

public size: * source

public type: string source

public url: string source

public width: number source

Private Members

private _$img: * source

private _origUrl: string source

private _removed: boolean source

Public Methods

public cleanup() source

Cleans up the <img> tag. Actually removes all WeChat-defined data-*, removes blank style and class attributes. And correct the URL to what you set, in case some of your transformation breaks it.

public get$Img(): * source

Get the underlying $(img). Good for custom tranformations. But don't set src manually there. Try ArticleImage#setUrl.

Return:

*

public isRemoved(): * source

See if you've already removed it.

Return:

*

public remove() source

Removes current image from the HTML. This is useful when you decided to remove some decorational gifs & images.

public setUrl(newUrl: *) source

Set src for the picture. Please make sure this is correctly pointed to where you store the image.

Params:

NameTypeAttributeDescription
newUrl *

public async stream(): Stream source

Returns a stream of that iamges. You can use this to persist images to disk.

Return:

Stream

Example:

(await articleImage.stream()).pipe(fs.createWriteStream('xxx.jpg'))