Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import PullToRefresh from './pull-to-refresh'

export default function Layout ({
sub, contain = true, footer = true, footerLinks = true,
containClassName = '', seo = true, item, user, children
containClassName = '', seo = true, item, user, territory, children
}) {
return (
<>
{seo && <Seo sub={sub} item={item} user={user} />}
{seo && <Seo sub={sub} item={item} user={user} territory={territory} />}
<Navigation sub={sub} />
{contain
? (
Expand Down
39 changes: 9 additions & 30 deletions components/seo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NextSeo } from 'next-seo'
import { useRouter } from 'next/router'
import removeMd from 'remove-markdown'
import { numWithUnits } from '@/lib/format'
import { processTerritorySEODescription, processItemSEODescription, processUserSEODescription } from '@/lib/seo'

export function SeoSearch ({ sub }) {
const router = useRouter()
Expand Down Expand Up @@ -31,45 +30,25 @@ export function SeoSearch ({ sub }) {
)
}

// for a sub we need
// item seo
// index page seo
// recent page seo

export default function Seo ({ sub, item, user }) {
export default function Seo ({ sub, item, user, territory }) {
const router = useRouter()
const pathNoQuery = router.asPath.split('?')[0]
const defaultTitle = pathNoQuery.slice(1)
const snStr = `stacker news${sub ? ` ~${sub}` : ''}`
let fullTitle = `${defaultTitle && `${defaultTitle} \\ `}stacker news`
let desc = "It's like Hacker News but we pay you Bitcoin."
if (item) {
if (territory) {
fullTitle = `${territory.name} \\ ${snStr}`
desc = processTerritorySEODescription(territory)
} else if (item) {
if (item.title) {
fullTitle = `${item.title} \\ ${snStr}`
} else if (item.root) {
fullTitle = `reply on: ${item.root.title} \\ ${snStr}`
}
// at least for now subs (ie the only one is jobs) will always have text
if (item.text) {
desc = removeMd(item.text)
if (desc) {
desc = desc.replace(/\s+/g, ' ')
}
} else {
desc = `@${item.user.name} stacked ${numWithUnits(item.sats)} ${item.url ? `posting ${item.url}` : 'with this discussion'}`
}
if (item.ncomments) {
desc += ` [${numWithUnits(item.ncomments, { unitSingular: 'comment', unitPlural: 'comments' })}`
if (item.boost) {
desc += `, ${item.boost} boost`
}
desc += ']'
} else if (item.boost) {
desc += ` [${item.boost} boost]`
}
}
if (user) {
desc = `@${user.name} has [${user.optional.stacked ? `${user.optional.stacked} stacked,` : ''}${numWithUnits(user.nitems, { unitSingular: 'item', unitPlural: 'items' })}]`
desc = processItemSEODescription(item)
} else if (user) {
desc = processUserSEODescription(user)
}

return (
Expand Down
198 changes: 198 additions & 0 deletions lib/seo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import removeMd from 'remove-markdown'
import { numWithUnits } from './format'

export function cleanMarkdownText (text) {
if (!text) return ''

let cleaned = removeMd(text)
cleaned = cleaned
.replace(/\s+/g, ' ') // Replace multiple spaces with single space
// Handle nested markdown links like [[text]](url) - remove outer brackets and keep inner text
.replace(/\[\[([^\]]+)\]\]\([^)]+\)/g, '$1') // Remove nested markdown links, keep inner text
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1') // Remove regular markdown links, keep text
.replace(/!\[([^\]]*)\]\([^)]+\)/g, '') // Remove images completely
.replace(/`([^`]+)`/g, '$1') // Remove code formatting
.replace(/\*\*([^*]+)\*\*/g, '$1') // Remove bold formatting
.replace(/\*([^*]+)\*/g, '$1') // Remove italic formatting
.trim()

return cleaned
}

export function truncateAtSentenceBoundary (text, maxLength = 160) {
if (!text || text.length <= maxLength) return text

const sentences = text
.split(/[.!?]+/)
.map(s => s.trim())
.filter(Boolean)

let result = ''
let currentLength = 0

for (const sentence of sentences) {
const separator = result ? '. ' : ''
const newLength = currentLength + separator.length + sentence.length

if (newLength <= maxLength - 3) {
result += separator + sentence
currentLength = newLength
} else {
break
}
}

if (result) {
return result + '...'
}

// Fallback to simple truncation
return text.substring(0, maxLength - 3) + '...'
}

export function processDescription (text, options = {}) {
const { minLength = 30, maxLength = 160, fallback = null } = options

if (!text) return fallback

const processed = cleanMarkdownText(text)

if (processed.length >= minLength && processed.length <= maxLength) {
return processed
}

if (processed.length > maxLength) {
return truncateAtSentenceBoundary(processed, maxLength)
}

return fallback
}

export function processItemSEODescription (item) {
if (item.text) {
const processed = processDescription(item.text, {
minLength: 30,
maxLength: 160,
fallback: generateItemDescription(item)
})

if (processed) return processed
}

return generateItemDescription(item)
}

function generateItemDescription (item) {
const parts = []

if (item.user?.name) {
parts.push(`@${item.user.name}`)
}

if (item.sats > 0) {
parts.push(`stacked ${numWithUnits(item.sats)}`)
}

if (item.url) {
parts.push(`posting ${item.url}`)
} else {
parts.push('with this discussion')
}

if (item.ncomments) {
parts.push(`${numWithUnits(item.ncomments, { unitSingular: 'comment', unitPlural: 'comments' })})`)
}

if (item.boost) {
parts.push(`${item.boost} boost`)
}

return parts.join(' ')
}

export function processUserSEODescription (user) {
if (user.bio?.text) {
const processed = processDescription(user.bio.text, {
minLength: 30,
maxLength: 160,
fallback: generateUserDescription(user)
})

if (processed) return processed
}

return generateUserDescription(user)
}

function generateUserDescription (user) {
const parts = []

if (user.optional?.stacked) {
parts.push(`${user.optional.stacked} stacked`)
}

if (user.nitems) {
parts.push(`${numWithUnits(user.nitems, { unitSingular: 'item', unitPlural: 'items' })})`)
}

if (parts.length > 0) {
return `@${user.name} has [${parts.join(', ')}]`
}

return `@${user.name}`
}

export function processTerritorySEODescription (territory) {
if (!territory) return null

if (territory.desc) {
const processedDesc = cleanMarkdownText(territory.desc)

if (processedDesc.length >= 30 && processedDesc.length <= 160) {
return processedDesc
}
if (processedDesc.length > 160) {
return truncateAtSentenceBoundary(processedDesc, 160)
}
}

// Generate a contextual description if the original is too short or unsuitable
return generateTerritoryDescription(territory)
}

function generateTerritoryDescription (territory) {
const parts = []
if (territory.desc) {
const processedDesc = cleanMarkdownText(territory.desc)
if (processedDesc) {
parts.push(processedDesc)
}
}

if (territory.postTypes && territory.postTypes.length > 0) {
const postTypeLabels = territory.postTypes.map(type => {
switch (type) {
case 'LINK': return 'links'
case 'DISCUSSION': return 'discussions'
case 'BOUNTY': return 'bounties'
case 'POLL': return 'polls'
default: return type.toLowerCase()
}
})
parts.push(postTypeLabels.join(', '))
}

if (territory.user?.name) {
parts.push(`created by @${territory.user.name}`)
}

let desc = parts.length > 1
? `Stacker.news community: ${parts.join('. ')}`
: `Stacker.news community for ${parts.join(', ')}`

if (desc.length > 160) {
desc = truncateAtSentenceBoundary(desc, 160)
}

return desc
}
2 changes: 1 addition & 1 deletion pages/~/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function Sub ({ ssrData }) {
const { sub } = data || ssrData

return (
<Layout sub={sub?.name}>
<Layout sub={sub?.name} territory={sub}>
{sub
? <TerritoryHeader sub={sub} />
: (
Expand Down