Skip to content

AI-native design editor. Open-source Figma alternative.

6.9 KB
Raw Blame
import { existsSync, readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

import * as v from 'valibot'
import type { HeadConfig } from 'vitepress'

export const BASE = 'https://openpencil.dev'

const docsRoot = fileURLToPath(new URL('..', import.meta.url))

const RootManifestSchema = v.pipe(v.string(), v.parseJson(), v.object({ version: v.string() }))
const { version } = v.parse(
  RootManifestSchema,
  readFileSync(resolve(docsRoot, '../../package.json'), 'utf8')
)

/**
 * Search engines read the product from the English home page. It is written into the page's
 * head at build time, so it never takes part in hydration.
 */
const SOFTWARE_APPLICATION = {
  '@context': 'https://schema.org',
  '@type': 'SoftwareApplication',
  name: 'OpenPencil',
  applicationCategory: 'DesignApplication',
  operatingSystem: 'Windows, macOS, Linux, Web',
  offers: { '@type': 'Offer', price: '0', priceCurrency: 'USD' },
  url: 'https://app.openpencil.dev',
  description:
    'Open-source design editor that opens Figma .fig files, runs locally, and is programmable by you or your AI agent.',
  softwareVersion: version,
  license: 'https://opensource.org/licenses/MIT',
  screenshot: `${BASE}/screenshot.png`,
  downloadUrl: `${BASE}/download`,
  featureList: [
    'Opens and saves Figma .fig files',
    'AI agents that design on the canvas with your own provider keys',
    'MCP server for AI coding agents',
    'Real-time collaboration with people and agents',
    'Headless CLI for inspection, linting, and export',
    'Desktop app for macOS, Windows, and Linux',
    'Vue SDK for embedding the editor'
  ]
}

export const LOCALE_PREFIXES = ['de', 'fr', 'es', 'it', 'pl', 'ru'] as const

export const LOCALES: Record<string, { hreflang: string; ogLocale: string; prefix: string }> = {
  en: { hreflang: 'en', ogLocale: 'en_US', prefix: '' },
  de: { hreflang: 'de', ogLocale: 'de_DE', prefix: '/de' },
  fr: { hreflang: 'fr', ogLocale: 'fr_FR', prefix: '/fr' },
  es: { hreflang: 'es', ogLocale: 'es_ES', prefix: '/es' },
  it: { hreflang: 'it', ogLocale: 'it_IT', prefix: '/it' },
  pl: { hreflang: 'pl', ogLocale: 'pl_PL', prefix: '/pl' },
  ru: { hreflang: 'ru', ogLocale: 'ru_RU', prefix: '/ru' }
}

export const siteHead: [string, Record<string, string>][] = [
  ['link', { rel: 'icon', href: '/favicon.ico', sizes: 'any' }],
  ['link', { rel: 'icon', type: 'image/png', href: '/brand/favicon-96x96.png', sizes: '96x96' }],
  ['link', { rel: 'icon', type: 'image/svg+xml', href: '/brand/favicon.svg' }],
  ['link', { rel: 'apple-touch-icon', href: '/apple-touch-icon.png' }],
  ['link', { rel: 'alternate', type: 'text/plain', title: 'llms.txt', href: '/llms.txt' }],
  [
    'link',
    { rel: 'alternate', type: 'text/plain', title: 'llms-full.txt', href: '/llms-full.txt' }
  ],
  ['meta', { property: 'og:type', content: 'website' }],
  ['meta', { property: 'og:site_name', content: 'OpenPencil' }],
  ['meta', { property: 'og:image', content: `${BASE}/social.png` }],
  ['meta', { property: 'og:image:width', content: '2400' }],
  ['meta', { property: 'og:image:height', content: '1260' }],
  ['meta', { property: 'og:image:alt', content: 'OpenPencil — Open-Source Design Editor' }],
  ['meta', { name: 'twitter:card', content: 'summary_large_image' }],
  ['meta', { name: 'twitter:site', content: '@openpencildev' }],
  ['meta', { name: 'twitter:image', content: `${BASE}/social.png` }]
]

type SitemapItem = {
  url: string
  links?: Array<{ lang: string; url: string }>
}

type PageDataLike = {
  relativePath: string
  title?: string
  description?: string
  frontmatter: {
    head?: HeadConfig[]
  }
}

function stripLocalePrefix(path: string): string {
  return path.replace(new RegExp(`^(${LOCALE_PREFIXES.join('|')})/`), '')
}

function localeKeyForPath(path: string): string {
  return LOCALE_PREFIXES.find((prefix) => path.startsWith(`${prefix}/`)) ?? 'en'
}

function slugForPath(path: string): string {
  return stripLocalePrefix(path)
    .replace(/\.md$/, '')
    .replace(/\/index$/, '')
    .replace(/^index$/, '')
    .replace(/\/$/, '')
}

function localizedUrl(slug: string, prefix: string): string {
  if (slug) return `${BASE}${prefix}/${slug}`
  return `${BASE}${prefix || ''}`
}

function availableLocalesForPage(relativePath: string) {
  const canonicalPath = stripLocalePrefix(relativePath)

  return Object.values(LOCALES).filter((locale) => {
    const localeDirectory = locale.prefix.replace(/^\//, '')
    return existsSync(resolve(docsRoot, localeDirectory, canonicalPath))
  })
}

export function withAlternateSitemapLinks<T extends SitemapItem>(items: T[]): T[] {
  const linksBySlug = new Map<string, Array<{ lang: string; url: string }>>()

  for (const item of items) {
    const path = new URL(item.url, BASE).pathname.replace(/^\//, '')
    const slug = slugForPath(path)
    const localeKey = localeKeyForPath(path)
    const locale = LOCALES[localeKey]
    const links = linksBySlug.get(slug) ?? []
    links.push({ lang: locale.hreflang, url: new URL(item.url, BASE).href })
    linksBySlug.set(slug, links)
  }

  return items.map((item) => {
    const path = new URL(item.url, BASE).pathname.replace(/^\//, '')
    return { ...item, links: linksBySlug.get(slugForPath(path)) }
  })
}

export function applyPageSeo(pageData: PageDataLike): void {
  const localeKey = localeKeyForPath(pageData.relativePath)
  const locale = LOCALES[localeKey]
  const slug = slugForPath(pageData.relativePath)
  const pageUrl = localizedUrl(slug, locale.prefix)
  const enSlug = slug ? `${BASE}/${slug}` : BASE

  pageData.frontmatter.head ??= []
  const head = pageData.frontmatter.head

  head.push(['link', { rel: 'canonical', href: pageUrl }])
  head.push(['meta', { property: 'og:url', content: pageUrl }])
  head.push(['meta', { property: 'og:locale', content: locale.ogLocale }])
  if (pageData.relativePath === 'index.md') {
    head.push(['script', { type: 'application/ld+json' }, JSON.stringify(SOFTWARE_APPLICATION)])
  }

  for (const localeOption of availableLocalesForPage(pageData.relativePath)) {
    if (localeOption.prefix !== locale.prefix) {
      head.push(['meta', { property: 'og:locale:alternate', content: localeOption.ogLocale }])
    }

    head.push([
      'link',
      {
        rel: 'alternate',
        hreflang: localeOption.hreflang,
        href: localizedUrl(slug, localeOption.prefix)
      }
    ])
  }
  head.push(['link', { rel: 'alternate', hreflang: 'x-default', href: enSlug }])

  if (pageData.title) {
    const ogTitle = `${pageData.title} — OpenPencil`
    head.push(['meta', { property: 'og:title', content: ogTitle }])
    head.push(['meta', { name: 'twitter:title', content: ogTitle }])
  }

  if (pageData.description) {
    head.push(['meta', { property: 'og:description', content: pageData.description }])
    head.push(['meta', { name: 'twitter:description', content: pageData.description }])
    head.push(['meta', { name: 'description', content: pageData.description }])
  }
}