Skip to content

Commit

Permalink
Configureのリファクタ
Browse files Browse the repository at this point in the history
  • Loading branch information
h-sugawara committed Nov 21, 2023
1 parent 7de554c commit 53e0ce7
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ const ANCHOR_LINK_CLASS_NAME = 'link-preview';
const DESCRIPTION_LENGTH = 140;
const DISGUISE_CRAWLER = true;

function getClassName(classNameFromHexoCfg, defaultAnchorLink) {
const obj = { anchor_link: defaultAnchorLink };

if (typeof classNameFromHexoCfg === 'string') {
obj.anchor_link = classNameFromHexoCfg || obj.anchor_link;
} else if (typeof classNameFromHexoCfg === 'object') {
if (hasProperty(classNameFromHexoCfg, 'anchor_link') && typeof classNameFromHexoCfg.anchor_link === 'string') {
obj.anchor_link = classNameFromHexoCfg.anchor_link || obj.anchor_link;
}

if (hasProperty(classNameFromHexoCfg, 'image') && classNameFromHexoCfg.image !== '') {
obj.image = classNameFromHexoCfg.image;
}
}

return obj;
}

module.exports = hexoCfg => {
const config = {
class_name: {
anchor_link: ANCHOR_LINK_CLASS_NAME,
},
class_name: { anchor_link: ANCHOR_LINK_CLASS_NAME },
description_length: DESCRIPTION_LENGTH,
disguise_crawler: DISGUISE_CRAWLER,
};
Expand All @@ -22,25 +38,11 @@ module.exports = hexoCfg => {
const hexoCfgLinkPreview = hexoCfg.link_preview;

if (hasProperty(hexoCfgLinkPreview, 'class_name')) {
const hexoCfgLinkPreviewClassName = hexoCfgLinkPreview.class_name;

if (typeof hexoCfgLinkPreviewClassName === 'string') {
config.class_name.anchor_link = hexoCfgLinkPreviewClassName || config.class_name.anchor_link;
} else if (typeof hexoCfgLinkPreviewClassName === 'object') {
if (hasProperty(hexoCfgLinkPreviewClassName, 'anchor_link') && typeof hexoCfgLinkPreviewClassName.anchor_link === 'string') {
config.class_name.anchor_link = hexoCfgLinkPreviewClassName.anchor_link || config.class_name.anchor_link;
}

if (hasProperty(hexoCfgLinkPreviewClassName, 'image') && hexoCfgLinkPreviewClassName.image !== '') {
config.class_name.image = hexoCfgLinkPreviewClassName.image;
}
}
config.class_name = getClassName(hexoCfgLinkPreview.class_name, config.class_name.anchor_link);
}

if (hasProperty(hexoCfgLinkPreview, 'description_length') && typeof hexoCfgLinkPreview.description_length === 'number') {
config.description_length = hexoCfgLinkPreview.description_length || config.description_length;
}

if (hasProperty(hexoCfgLinkPreview, 'disguise_crawler') && typeof hexoCfgLinkPreview.disguise_crawler === 'boolean') {
config.disguise_crawler = hexoCfgLinkPreview.disguise_crawler;
}
Expand Down

0 comments on commit 53e0ce7

Please sign in to comment.