Skip to content

Commit

Permalink
Add flag to fix unwrapping of excerpt HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
meduzen committed Nov 11, 2023
1 parent 74c9295 commit 44a2b20
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions vitepress/.vitepress/theme/utils/frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ const EMPTY_LINK_SELECTOR = 'a[href="./.html"]'

/**
* Turn excerpt into a proper link to be used in an index.
* @param {import('vitepress').ContentData} note
* @param {import('vitepress').ContentData} post
*/
export const excerptToLink = note => {
if (!note.frontmatter.excerpt) { return }
export const excerptToLink = post => {
if (!post.frontmatter.excerpt || post.excerptTransformed) { return }

const $body = parser.parseFromString(note.frontmatter.excerpt, 'text/html').body
const $linkToNote = $body.querySelector(EMPTY_LINK_SELECTOR)
const $body = parser.parseFromString(post.frontmatter.excerpt, 'text/html').body
const $linkToPost = $body.querySelector(EMPTY_LINK_SELECTOR)

// Add missing `href`, or use title as link add missing link.
if ($linkToNote) {
$linkToNote.href = note.url
if ($linkToPost) {
$linkToPost.href = post.url
} else {
$body.innerHTML = `<p><a href="${note.url}">${$body.children[0].innerHTML}</a></p>`
$body.innerHTML = `<p><a href="${post.url}">${$body.children[0].innerHTML}</a></p>`
}

/**
* Unwrap the `<p>` to only keep it’s HTML string. This would not be needed
* if Vue could insert raw (unescaped) HTML like Laravel {{! htmlStr !}}.
* That is why we wrapped the link by a `<p>` in a previous condition.
*/
note.frontmatter.excerpt = $body.children[0].innerHTML
post.frontmatter.excerpt = $body.children[0].innerHTML
post.excerptTransformed = true
}

0 comments on commit 44a2b20

Please sign in to comment.