Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
kravhen committed Dec 18, 2024
1 parent c96a4d2 commit 41e362e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
4 changes: 2 additions & 2 deletions components/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ function P ({ children, node, onlyImages, somethingBefore, somethingAfter, ...pr
)
}

function Details({ children, node, ...props }) {
function Details ({ children, node, ...props }) {
return (
<details className={styles.details} {...props}>
{children}
</details>
)
}

function Summary({ children, node, ...props }) {
function Summary ({ children, node, ...props }) {
return (
<summary className={styles.summary} {...props}>
{children}
Expand Down
39 changes: 39 additions & 0 deletions components/text.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,42 @@
}

/* Details/Summary styling */
.details {
border-left: 2px solid var(--theme-quoteBar);
padding-left: 0.75rem;
margin: calc(var(--grid-gap) * 0.5) 0;
transition: border-color 0.2s ease;
}

.details[open] {
border-left-color: #f7931a;
}

.summary {
cursor: pointer;
user-select: none;
color: #8b949e;
transition: color 0.2s ease;
padding: 0.25rem 0;
}

.summary:hover {
color: #ffd700;
}


.summary::marker,
.summary::-webkit-details-marker {
color: #8b949e;
}

.details[open] > .summary::marker,
.details[open] > .summary::-webkit-details-marker {
color: #f7931a;
}


.details > *:not(.summary) {
margin-top: calc(var(--grid-gap) * 0.5);
padding-bottom: calc(var(--grid-gap) * 0.25);
}
50 changes: 20 additions & 30 deletions lib/rehype-sn.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ const mentionRegex = new RegExp('@(' + userGroup + '(?:\\/' + userGroup + ')?)',
const subRegex = new RegExp('~(' + subGroup + '(?:\\/' + subGroup + ')?)', 'gi')
const nostrIdRegex = /\b((npub1|nevent1|nprofile1|note1|naddr1)[02-9ac-hj-np-z]+)\b/g

export default function rehypeSN(options = {}) {
export default function rehypeSN (options = {}) {
const { stylers = [] } = options

return function transformer(tree) {
return function transformer (tree) {
try {
visit(tree, (node, index, parent) => {

// Leave all other existing handlers unchanged
// Handle inline code property
if (node.tagName === 'code') {
node.properties.inline = !(parent && parent.tagName === 'pre')
}
Expand Down Expand Up @@ -233,7 +232,7 @@ export default function rehypeSN(options = {}) {
// Handle self-contained details block
if (node.value.includes('</details>')) {
let content = node.value

// Extract summary if present
const summaryMatch = content.match(/<summary>(.*?)<\/summary>/s)
if (summaryMatch) {
Expand All @@ -244,13 +243,13 @@ export default function rehypeSN(options = {}) {
detailsContent.summary.complete = true
content = content.replace(/<summary>.*?<\/summary>/s, '')
}

// Clean remaining content
const cleanedContent = content
.replace(/<details>/g, '')
.replace(/<\/details>/g, '')
.trim()

if (cleanedContent) {
detailsContent.content.push({
type: 'text',
Expand All @@ -263,7 +262,7 @@ export default function rehypeSN(options = {}) {

// Clean opening details tag and handle potential summary
let cleanedContent = node.value.replace(/<details>/g, '')

// Check for summary in opening node
const summaryMatch = cleanedContent.match(/<summary>(.*?)<\/summary>/s)
if (summaryMatch) {
Expand All @@ -274,7 +273,7 @@ export default function rehypeSN(options = {}) {
detailsContent.summary.complete = true
cleanedContent = cleanedContent.replace(/<summary>.*?<\/summary>/s, '')
}

if (cleanedContent.trim()) {
detailsContent.content.push({
type: 'text',
Expand All @@ -285,11 +284,11 @@ export default function rehypeSN(options = {}) {
// Collect remaining content
let currentIndex = index
let foundClosing = false

while (currentIndex < parent.children.length) {
const currentNode = parent.children[++currentIndex]
if (!currentNode) break

// Handle summary tags if we haven't found a complete summary yet
if (!detailsContent.summary.complete) {
if (currentNode.type === 'raw' && currentNode.value.includes('<summary>')) {
Expand All @@ -316,7 +315,7 @@ export default function rehypeSN(options = {}) {
const afterSummary = currentNode.value.substring(
currentNode.value.indexOf('</summary>') + '</summary>'.length
).trim()

if (afterSummary) {
detailsContent.content.push({
type: 'text',
Expand All @@ -335,7 +334,7 @@ export default function rehypeSN(options = {}) {
}
continue
}

// If we're collecting summary content
if (detailsContent.summary.found) {
if (currentNode.type === 'raw' && currentNode.value.includes('</summary>')) {
Expand All @@ -360,9 +359,8 @@ export default function rehypeSN(options = {}) {
// Check for closing details tag
const hasClosingTag = (currentNode.type === 'raw' && currentNode.value.includes('</details>')) ||
(currentNode.type === 'element' && toString(currentNode).includes('</details>'))

if (hasClosingTag) {
let cleanedContent
if (currentNode.type === 'raw') {
const textBeforeClosing = currentNode.value.substring(0, currentNode.value.indexOf('</details>'))
if (textBeforeClosing.includes('\n')) {
Expand Down Expand Up @@ -418,18 +416,17 @@ export default function rehypeSN(options = {}) {

return createDetailsElement(detailsContent, parent, index)
}

})

return tree
} catch (error) {
console.error('Error in rehypeSN transformer:', error)
console.error('Error in rehypeSN transformer:', error)
return tree
}
}
}

function isImageOnlyParagraph(node) {
function isImageOnlyParagraph (node) {
return node &&
node.tagName === 'p' &&
Array.isArray(node.children) &&
Expand All @@ -439,7 +436,7 @@ function isImageOnlyParagraph(node) {
)
}

function replaceMention(value, username) {
function replaceMention (value, username) {
return {
type: 'element',
tagName: 'mention',
Expand All @@ -448,7 +445,7 @@ function replaceMention(value, username) {
}
}

function replaceSub(value, sub) {
function replaceSub (value, sub) {
return {
type: 'element',
tagName: 'sub',
Expand All @@ -457,7 +454,7 @@ function replaceSub(value, sub) {
}
}

function replaceNostrId(value, id) {
function replaceNostrId (value, id) {
return {
type: 'element',
tagName: 'a',
Expand All @@ -466,7 +463,7 @@ function replaceNostrId(value, id) {
}
}

function isMisleadingLink(text, href) {
function isMisleadingLink (text, href) {
let misleading = false

if (/^\s*(\w+\.)+\w+/.test(text)) {
Expand All @@ -482,12 +479,7 @@ function isMisleadingLink(text, href) {
}

// Helper to create details element
function createDetailsElement(detailsContent, parent, index) {
console.log('\n🔨 Creating details element with:', {
hasSummary: detailsContent.summary.complete,
contentNodes: detailsContent.content.length
})

function createDetailsElement (detailsContent, parent, index) {
const detailsElement = {
type: 'element',
tagName: 'details',
Expand All @@ -504,12 +496,10 @@ function createDetailsElement(detailsContent, parent, index) {
children: detailsContent.summary.content
}
detailsElement.children.push(summaryElement)
console.log('✨ Added summary element')
}

// Add main content
detailsElement.children.push(...detailsContent.content)
console.log('✨ Added content elements')

// Replace nodes
parent.children.splice(
Expand Down

0 comments on commit 41e362e

Please sign in to comment.