Coder Social home page Coder Social logo

l422y / nuxt-head-ex Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 0.0 205 KB

Implements `useHeadEx()` composable to automatically propagate title and description to social media compatible meta tags.

Vue 26.43% TypeScript 73.57%
meta-tags nuxt nuxtjs open-graph seo seo-optimization social-media vue

nuxt-head-ex's Introduction

Nuxt: HeadExtra

npm

Implements useHeadEx() composable to automatically propagate title and description to social media compatible meta tags.

yarn add nuxt-head-ex

...and add the module to your nuxt.config.ts:

 modules: ['nuxt-head-ex'],

Configuration example (see types.ts for more information / parameters):

export default defineNuxtConfig({
  modules: ['nuxt-head-ex'],
  headExtra: {
    extra: 'My Sweet Website', // appended to titles
    separator: '•', // used to separate title components / extra
    defaults: {
      // you can use {{fullPath}} to pass the path to a dynamic image generator
      socialImageURL: 'https://l422y.com/images/share.png?path={{fullPath}}',
      description: 'Senior full-stack engineer and creative technologist with over 20 years’ experience and a focus in software, interactive and web development.'
    }
  }
})

You can override the title rendering function altogether:

<script setup>
  const app = useNuxtApp()
  app.$headExtra.renderTitle = function ({ title, subtitle, section, separator, extra }) {
    let renderedTitle = `${section && section?.length > 0 ? ` ${separator} ${section}` : ''}${extra && extra.length > 0 ? (title ? `  ${separator} ` : '') + extra : ''}`
    if (title) {
      renderedTitle = `⚡️ ${title}${renderedTitle}`
    } else {
      title = renderedTitle
    }
  return renderedTitle
}
</script>

Usage:

<script setup>
  useHeadEx({
  title: `${project?.title}`,
  subtitle,
  section: `PROJECTS`,
  description: `${excerpt}`
})
</script>

Accessing updated values:

const nuxt = useNuxtApp()
const sectionTitle = useState('sectionTitle')

// set up a callback
nuxt.$headExtra.callback = (headObj) => { /*...*/
}

// ...or use the `headExtra:update` nuxt hook
nuxt.hook('headExtra:update', (headObj) => {
  sectionTitle = headObj.meta.find(v => v.name === 'clean:section')
})

Accessing the currently used input reflectively:

app.vue

<template>
  <div v-if="hExValues?.section">
    hExValues.section = "{{ hExValues.section }}"
  </div>
  <NuxtPage/>
</template>
<script setup>
  const hExValues = useState('headExtraValues')
</script>

pages/test.vue

<template>
  <section id="testing">
    Now we're cooking with gas!
  </section>
</template>
<script setup>
  useHeadEx({
      title: 'Testing!', 
      section: 'Testing Section'
  })
</script>

Development

  • Run npm run dev:prepare to generate type stubs.
  • Use npm run dev to start playground in development mode.

Credits

Made with 💚 by Larry Williamson / @l422y

nuxt-head-ex's People

Contributors

l422y avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.