Skip to content

Commit

Permalink
fix: fix icon and type is required when override built-in network
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnyq committed Jan 5, 2025
1 parent 1bb6f59 commit 27c75f0
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 94 deletions.
42 changes: 24 additions & 18 deletions docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,9 @@ import { defineUserConfig } from 'vuepress'
import { socialSharePlugin } from 'vuepress-plugin-social-share'
import { version } from '../../package.json'
import type { NavbarLinkOptions } from '@vuepress/theme-default'
import type { SocialShareNetwork } from 'vuepress-plugin-social-share'

const packageName = 'vuepress-plugin-social-share'

const extendsNetworks: Record<string, SocialShareNetwork> = {
pinterest: {
sharer: 'https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title',
type: 'popup',
icon: '/pinterest.png',
},
linkedin: {
sharer:
'https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description',
type: 'popup',
color: '#1786b1',
icon: '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M910.336 0H113.664A114.005333 114.005333 0 0 0 0 113.664v796.672A114.005333 114.005333 0 0 0 113.664 1024h796.672A114.005333 114.005333 0 0 0 1024 910.336V113.664A114.005333 114.005333 0 0 0 910.336 0zM352.256 796.330667H207.189333V375.466667h145.066667z m-72.021333-477.866667a77.824 77.824 0 0 1-81.237334-74.069333A77.824 77.824 0 0 1 280.234667 170.666667a77.824 77.824 0 0 1 81.237333 73.728 77.824 77.824 0 0 1-81.237333 73.386666z m582.314666 477.866667H716.8v-227.669334c0-46.762667-18.432-93.525333-73.045333-93.525333a84.992 84.992 0 0 0-81.237334 94.549333v226.304h-140.629333V375.466667h141.653333v60.757333a155.989333 155.989333 0 0 1 136.533334-71.338667c60.416 0 163.498667 30.378667 163.498666 194.901334z" /></svg>',
},
}

const VERSIONS: NavbarLinkOptions[] = [
{ text: `v${version} (current)`, link: '/' },
{ text: `Release Notes`, link: `https://github.com/ntnyq/${packageName}/releases` },
Expand Down Expand Up @@ -80,12 +64,34 @@ export default defineUserConfig({
theme: 'one-dark-pro',
}),
socialSharePlugin({
networks: ['qq', 'twitter', 'weibo', 'email', 'linkedin', 'pinterest', 'wechat'],
networks: [
'qq',
'twitter',
'weibo',
'email',
'wechat',
{
name: 'pinterest',
type: 'popup',
icon: '/pinterest.png',
default: true,
sharer:
'https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title',
},
{
name: 'linkedin',
type: 'popup',
color: '#1786b1',
default: true,
sharer:
'https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description',
icon: '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M910.336 0H113.664A114.005333 114.005333 0 0 0 0 113.664v796.672A114.005333 114.005333 0 0 0 113.664 1024h796.672A114.005333 114.005333 0 0 0 1024 910.336V113.664A114.005333 114.005333 0 0 0 910.336 0zM352.256 796.330667H207.189333V375.466667h145.066667z m-72.021333-477.866667a77.824 77.824 0 0 1-81.237334-74.069333A77.824 77.824 0 0 1 280.234667 170.666667a77.824 77.824 0 0 1 81.237333 73.728 77.824 77.824 0 0 1-81.237333 73.386666z m582.314666 477.866667H716.8v-227.669334c0-46.762667-18.432-93.525333-73.045333-93.525333a84.992 84.992 0 0 0-81.237334 94.549333v226.304h-140.629333V375.466667h141.653333v60.757333a155.989333 155.989333 0 0 1 136.533334-71.338667c60.416 0 163.498667 30.378667 163.498666 194.901334z" /></svg>',
},
],
twitterUser: 'ntnyq',
fallbackImage: '/hero.png',
useCustomStyle: false,
hideWhenPrint: true,
extendsNetworks,
}),
],
})
37 changes: 12 additions & 25 deletions docs/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,24 @@ export default defineUserConfig({

### networks

- **type:** `(string | SocialShareNetworkWithName)[]`
- **type:** `(string | SocialShareNetworkWithName | OverrideSocialShareNetworkWithName)[]`
- **default** `['twitter', 'facebook', 'reddit']`

```ts
type ThemeableValue<T = string> = T | { light: T; dark: T }
type OverrideSocialShareNetworkWithName = Partial<SocialShareNetworkWithName> & {
name: BuiltInNetworkNames
}
type SocialShareNetworkWithName = {
/**
* Sharer name
*/
name: string

/**
* Sharer icon
*/
icon:
| string
| {
/**
* icon for dark mode
*/
dark: string
/**
* icon for light mode
*/
light: string
}
icon: ThemeableValue<string>

/**
* Sharer type
Expand All @@ -128,18 +126,7 @@ type SocialShareNetworkWithName = {
/**
* Sharer icon color
*/
color?:
| string
| {
/**
* color for dark mode
*/
dark: string
/**
* color for light mode
*/
light: string
}
color?: ThemeableValue<string>

/**
* Sharer URL
Expand Down
17 changes: 10 additions & 7 deletions src/client/components/SocialShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,22 @@ export const SocialShare = defineComponent({
const body = document.body
const socialShareEl = document.querySelector('#__VUEPRESS_SOCIAL_SHARE__')
const socialShareOverlay = document.createElement('div')

socialShareOverlay.id = '__VUEPRESS_SOCIAL_SHARE__'
socialShareOverlay.classList.add('social-share-overlay')

if (socialShareEl && socialShareEl.parentNode) {
socialShareEl.remove()
}

try {
const QRCode = await import('qrcode')
const dataURL = await QRCode.toDataURL(url.value, qrcodeRenderOptions.value)

socialShareOverlay.innerHTML = `<img class="social-share-qrcode" src="${dataURL}" />`
body.append(socialShareOverlay)
socialShareOverlay.classList.add('show')

socialShareOverlay.addEventListener('click', evt => {
socialShareOverlay.classList.remove('show')
socialShareOverlay.remove()
Expand Down Expand Up @@ -233,18 +238,16 @@ export const SocialShare = defineComponent({
const onShare = (name: string) => {
const network = options.networksData.find(item => item.name === name)!
const shareURL = createShareURL(name, network)

switch (network.type) {
case 'popup':
openSharer(shareURL)
break
return openSharer(shareURL)
case 'qrcode':
showQRCode()
break
return showQRCode()
case 'direct':
openWindow(shareURL)
break
return openWindow(shareURL)
default:
break
return openSharer(shareURL)
}
}
const renderNetworkList = (networks: SocialShareNetworkWithName[]) =>
Expand Down
4 changes: 3 additions & 1 deletion src/client/components/SocialShareNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const SocialShareNetwork = defineComponent({
required: true,
validator: (network: SocialShareNetworkWithName) => {
if (!network.icon) return false
if (['popup'].includes(network.type)) return Boolean(network.sharer)
if (network.type !== 'qrcode') {
return Boolean(network.sharer)
}
return true
},
},
Expand Down
5 changes: 2 additions & 3 deletions src/node/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Logger } from '@vuepress/helper'
import deepmerge from 'deepmerge'
import { isString } from '../shared/index.js'
import { BUILT_IN_NETWORKS, PLUGIN_NAME } from './constants.js'
import { BUILT_IN_NETWORKS, isString, PLUGIN_NAME } from '../shared/index.js'
import type { SocialShareNetworkWithName, SocialSharePluginOptions } from '../shared/index.js'

export const logger = new Logger(PLUGIN_NAME)
Expand Down Expand Up @@ -31,7 +30,7 @@ export function resolveNetworksData(
if (mergedNetworkNames.has(network.name)) {
mergedNetworks[network.name] = deepmerge(mergedNetworks[network.name], network)
} else {
mergedNetworks[network.name] = network
mergedNetworks[network.name] = network as SocialShareNetworkWithName
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/node/socialSharePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addViteSsrNoExternal } from '@vuepress/helper'
import { getDirname, path } from 'vuepress/utils'
import { PLUGIN_NAME } from './constants.js'
import { PLUGIN_NAME } from '../shared/index.js'
import { logger, resolveNetworksData } from './helpers.js'
import type { PluginFunction } from 'vuepress/core'
import type {
Expand Down
2 changes: 1 addition & 1 deletion src/node/constants.ts → src/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SocialShareNetwork } from '../shared/index.js'
import type { SocialShareNetwork } from './network.js'

export const PLUGIN_NAME = 'vuepress-plugin-social-share'

Expand Down
1 change: 1 addition & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './utils.js'
export * from './plugin.js'
export * from './network.js'
export * from './constants.js'
52 changes: 16 additions & 36 deletions src/shared/network.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
/**
* network color
*/
export type SocialShareColor =
| string
| {
/**
* color for dark mode
*/
dark: string
/**
* color for light mode
*/
light: string
}

/**
* network type
*/
export type SocialShareType = 'direct' | 'popup' | 'qrcode'
import type { BuiltInNetworkNames } from './constants.js'

/**
* network icon
* Themeable value
*/
export type SocilaShareIcon =
| string
| {
/**
* icon for dark mode
*/
dark: string
/**
* icon for light mode
*/
light: string
}
export type ThemeableValue<T = string> = T | { light: T; dark: T }

/**
* Social share network
Expand All @@ -42,17 +12,17 @@ export interface SocialShareNetwork {
/**
* Sharer icon
*/
icon: SocilaShareIcon
icon: ThemeableValue

/**
* Sharer type
*/
type: SocialShareType
type: 'direct' | 'popup' | 'qrcode'

/**
* Sharer icon color
*/
color?: SocialShareColor
color?: ThemeableValue

/**
* Sharer URL
Expand All @@ -79,3 +49,13 @@ export type SocialShareNetworkWithName = SocialShareNetwork & {
*/
name: string
}

/**
* Override social share network with name
*/
export type OverrideSocialShareNetworkWithName = Partial<SocialShareNetwork> & {
/**
* Built-in share name
*/
name: BuiltInNetworkNames
}
8 changes: 6 additions & 2 deletions src/shared/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { QRCodeToDataURLOptions } from 'qrcode'
import type { SocialShareNetwork, SocialShareNetworkWithName } from './network'
import type {
OverrideSocialShareNetworkWithName,
SocialShareNetwork,
SocialShareNetworkWithName,
} from './network.js'

/**
* @deprecated use `Record<string, SocialShareNetwork>` instead
Expand Down Expand Up @@ -71,7 +75,7 @@ export interface SocialSharePluginOptions {
*
* @default ['twitter', 'facebook', 'reddit']
*/
networks?: (string | SocialShareNetworkWithName)[]
networks?: (string | SocialShareNetworkWithName | OverrideSocialShareNetworkWithName)[]

/**
* Twitter profile username
Expand Down

0 comments on commit 27c75f0

Please sign in to comment.