[{"data":1,"prerenderedAt":2460},["ShallowReactive",2],{"series-\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post":3,"series-context-\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch":2151},{"id":4,"title":5,"body":6,"contentId":2134,"date":2135,"description":2136,"extension":1649,"github":2137,"meetTheInstructor":2139,"meta":2140,"navigation":512,"part":370,"path":2141,"rawbody":2142,"robots":2139,"schemaOrg":2139,"seo":2143,"seriesSlug":2144,"sitemap":2145,"stem":2149,"subtitle":2139,"topics":2139,"__hash__":2150},"series\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post.md","Creating Open Graph Images Automatically for Each Blog Post",{"type":7,"value":8,"toc":2123},"minimark",[9,26,46,49,54,68,75,90,93,121,125,128,133,155,166,181,184,188,191,201,210,240,247,253,262,267,280,292,302,315,324,416,420,433,436,460,463,466,473,553,561,611,616,666,671,819,824,904,914,991,994,1517,1521,1524,1534,1537,1671,1682,1685,1823,1840,1848,1852,1867,2094,2098,2108,2111,2119],[10,11,12,13,17,18,21,22,25],"p",{},"Sharing content on ",[14,15,16],"strong",{},"social media"," is one of the most effective strategies to engage with a wider audience. When promoting a blog post, including an ",[14,19,20],{},"Open Graph image (OG image)"," is crucial for improving the post's visual appeal. These images are displayed when sharing links on platforms such as ",[14,23,24],{},"Twitter, Facebook, and LinkedIn",".",[27,28,31],"callout",{"color":29,"icon":30},"info","i-ph-info-duotone",[10,32,33,36,37,41,42,45],{},[14,34,35],{},"An OG image is a PNG or JPEG"," that should be at least 1200 x 630 pixels in size, less than 5MB, and referenced in the ",[38,39,40],"code",{},"\u003Cmeta property=\"og:image\" content=\"...\">"," tag within the HTML ",[38,43,44],{},"\u003Chead>"," section using an absolute URL.",[10,47,48],{},"Creating an OG image for each blog post manually can be time-consuming. Moreover, any design modifications necessitate updating all the OG images, which is impractical in the long run. It's essential to be agile in updating and evolving our blog efficiently without dedicating excessive time. Keep in mind our goal:  Writing is the primary focus, the rest is automated.",[50,51,53],"h2",{"id":52},"the-theory","The theory",[10,55,56,57,60,61,63,64,67],{},"To dynamically generate an OG image, we first need to create an ",[14,58,59],{},"SVG template",". In this template, placeholders will represent content like the page title, description, and author. Using a suitable library, we will convert this ",[14,62,59],{}," into a ",[14,65,66],{},"PNG image"," for each page, utilizing the designated placeholders.",[10,69,70,71,74],{},"These generated images will be added to the ",[38,72,73],{},".gitignore"," file, ensuring that each time we generate the website, the OG images are also created. During development, we check if an OG image already exists to avoid unnecessary computation.",[27,76,77],{"color":29,"icon":30},[10,78,79,80,89],{},"This approach was inspired by ",[81,82,86],"a",{"href":83,"rel":84},"https:\u002F\u002Fgithub.com\u002Fantfu\u002Fantfu.me\u002Fblob\u002Fmain\u002Fvite.config.ts#L242",[85],"nofollow",[14,87,88],{},"Anthony Fu's blog",". I have adapted it to suit our requirements.",[10,91,92],{},"The steps for generating the OG images are as follows:",[94,95,96,104,107,114],"ol",{},[97,98,99,100,103],"li",{},"Create an SVG template with ",[14,101,102],{},"Figma"," (or another tool of choice).",[97,105,106],{},"Export two versions: one with outlined text and one without.",[97,108,109,110,113],{},"Manually integrate the two SVG templates, preserving the outlined text while keeping the placeholders and ",[38,111,112],{},"\u003Ctext>"," tags from the original.",[97,115,116,117,120],{},"Implement a function in ",[14,118,119],{},"VitePress"," to dynamically create the OG image.",[50,122,124],{"id":123},"the-implementation","The implementation",[10,126,127],{},"Enough theory; let's delve into the implementation.",[129,130,132],"h3",{"id":131},"create-the-svg-template","Create the SVG template",[10,134,135,136,138,139,142,143,146,147,150,151,154],{},"In ",[14,137,102],{},", design an OG image template with dimensions of 1200 x 630 pixels. Define placeholders for the post title using ",[38,140,141],{},"{{line1}}",", ",[38,144,145],{},"{{line2}}",", and ",[38,148,149],{},"{{line3}}",", each accommodating up to 30 characters, allowing a total of 90 characters for most titles. Remember, ",[14,152,153],{},"SEO practices"," discourage titles exceeding 100 characters.",[156,157,158,163],"figure",{},[159,160],"img",{"alt":161,"src":162},"OG Image Template in Figma","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post\u002Fvitepress-figma-og.png",[164,165,161],"figcaption",{},[27,167,170],{"color":168,"icon":169},"success","i-ph-lightbulb-duotone",[10,171,172,173,178,179,25],{},"If you're uncertain about getting started, consider using ",[81,174,177],{"href":175,"rel":176},"https:\u002F\u002Fgithub.com\u002FBarbapapazes\u002Fgarabit\u002Fblob\u002Fmain\u002F.vitepress\u002Fog-template.svg",[85],"the template I created for this project"," and upload it to ",[14,180,102],{},[10,182,183],{},"Export two SVG files: one with outlined text and another without (ensure the option is unchecked). Both exports should be SVG files.",[129,185,187],{"id":186},"merge-the-two-svg-templates","Merge the two SVG templates",[10,189,190],{},"Upon exporting, we have two SVG files which need to be merged to develop our template.",[27,192,195],{"color":193,"icon":194},"warning","i-ph-warning-duotone",[10,196,197,198,25],{},"This is the most challenging part. Continue reading before starting to merge the ",[14,199,200],{},"SVG files",[10,202,203,204,206,207,209],{},"The smaller SVG contains readable text, which exists within ",[38,205,112],{}," tags. Retain these ",[38,208,112],{}," tags representing our placeholders:",[211,212,218],"pre",{"className":213,"code":214,"filename":215,"language":216,"meta":217,"style":217},"language-xml shiki shiki-themes github-light github-light github-dark","\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"198.318\">{{line1}}\u003C\u002Ftspan>\u003C\u002Ftext>\n\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"283.318\">{{line2}}\u003C\u002Ftspan>\u003C\u002Ftext>\n\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"368.318\">{{line3}}\u003C\u002Ftspan>\u003C\u002Ftext>\n","Example","xml","",[38,219,220,228,234],{"__ignoreMap":217},[221,222,225],"span",{"class":223,"line":224},"line",1,[221,226,227],{},"\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"198.318\">{{line1}}\u003C\u002Ftspan>\u003C\u002Ftext>\n",[221,229,231],{"class":223,"line":230},2,[221,232,233],{},"\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"283.318\">{{line2}}\u003C\u002Ftspan>\u003C\u002Ftext>\n",[221,235,237],{"class":223,"line":236},3,[221,238,239],{},"\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"368.318\">{{line3}}\u003C\u002Ftspan>\u003C\u002Ftext>\n",[10,241,242,243,246],{},"Open both SVG files side by side to simplify the merging process. Find the equivalent ",[38,244,245],{},"\u003Cpathx>"," tags in the larger file, which represent the outlined or vectorized text.",[10,248,249,250,252],{},"For example, the ",[38,251,141],{}," text transforms into the path:",[211,254,256],{"className":213,"code":255,"filename":215,"language":216,"meta":217,"style":217},"\u003Cpathx d=\"M141.982 180.358V176.629C144.396 176.629 146.08 176.139 172.602V179.761H357.958V172.602H364.712Z\" fill=\"black\"\u002F>\n",[38,257,258],{"__ignoreMap":217},[221,259,260],{"class":223,"line":224},[221,261,255],{},[27,263,264],{"color":29,"icon":30},[10,265,266],{},"The path example is shortened for clarity. Actual paths are considerably longer.",[10,268,269,270,273,274,276,277,279],{},"Keep the files open concurrently as text order corresponds in both. Replace ",[38,271,272],{},"\u003Cpath>"," tags in the second file with the ",[38,275,112],{}," tags from the first file. Retain any ",[38,278,272],{}," tags not associated with text placeholders.",[27,281,282],{"color":168,"icon":169},[10,283,284,285,287,288,291],{},"Assign a color to the placeholders in ",[14,286,102],{}," to easily identify them during merging. You can update the color in the SVG file via the ",[38,289,290],{},"fill"," property.",[10,293,294,295,25],{},"After combining, optimize the resultant SVG file using ",[81,296,299],{"href":297,"rel":298},"https:\u002F\u002Fjakearchibald.github.io\u002Fsvgomg\u002F",[85],[14,300,301],{},"SVGO",[10,303,304,305,307,308,311,312,25],{},"This refined SVG template is now ready for use in our ",[14,306,119],{}," project. Save it to the ",[38,309,310],{},".vitepress"," directory as ",[38,313,314],{},"og-template.svg",[27,316,317],{"color":168,"icon":169},[10,318,319,320,25],{},"Access the ",[81,321,323],{"href":175,"rel":322},[85],"final SVG template in the project repository",[211,325,327],{"className":213,"code":326,"filename":215,"language":216,"meta":217,"style":217},"\u003C!-- Partial SVG, DO NOT USE AS IS -->\n\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" fill=\"none\" viewBox=\"0 0 1200 630\">\n  \u003C!-- Template elements -->\n  \u003Cpath fill=\"#FA8EFD\" d=\"M0 0h1200v630H0z\"\u002F>\n  \u003Cpath fill=\"#000\" d=\"M96 96h1040v363H96zm310 415h407v69H406z\"\u002F>\n  \u003Cpath fill=\"#FEEA35\" d=\"M401 506h401v63H401z\"\u002F>\n  \u003Cpath stroke=\"#000\" stroke-width=\"6\" d=\"M401 506h401v63H401z\"\u002F>\n  \u003Cpath fill=\"#000\" d=\"M451.415 534.119a4.504 4.504 0 0 0-.588-1.235 3.79 3.79 0 0 0-.887-.946 3.571 3.571 0 0 0-1.176-.589 4.917 4.917 0 0 0-1.449-.204c-.937 0-1.772.236-2.505.707-.733.472-1.31 1.165-1.73 2.08-.415.909-.623 2.017-.623 1.679c.375.733.56 1.622.554 2.668V546h-3.085v-7.858c0-.875-.227-1.56-.682-2.054-.449-.494-1.071-.741-1.866-.741-.54 0-1.02.119-1.441.358a2.477 2.477 0 0 0-.98 1.014c-.233.443-.349.98-.349 1.611Z\"\u002F>\n  \u003Cpath fill=\"#fff\" d=\"M83 83h1034v357H83z\"\u002F>\n  \u003Cpath stroke=\"#000\" stroke-width=\"6\" d=\"M83 83h1034v357H83z\"\u002F>\n  \u003C!-- Text placeholders -->\n  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"198.318\">{{line1}}\u003C\u002Ftspan>\u003C\u002Ftext>\n  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"283.318\">{{line2}}\u003C\u002Ftspan>\u003C\u002Ftext>\n  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"368.318\">{{line3}}\u003C\u002Ftspan>\u003C\u002Ftext>\n\u003C\u002Fsvg>\n",[38,328,329,334,339,344,350,356,362,368,374,380,386,392,398,404,410],{"__ignoreMap":217},[221,330,331],{"class":223,"line":224},[221,332,333],{},"\u003C!-- Partial SVG, DO NOT USE AS IS -->\n",[221,335,336],{"class":223,"line":230},[221,337,338],{},"\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" fill=\"none\" viewBox=\"0 0 1200 630\">\n",[221,340,341],{"class":223,"line":236},[221,342,343],{},"  \u003C!-- Template elements -->\n",[221,345,347],{"class":223,"line":346},4,[221,348,349],{},"  \u003Cpath fill=\"#FA8EFD\" d=\"M0 0h1200v630H0z\"\u002F>\n",[221,351,353],{"class":223,"line":352},5,[221,354,355],{},"  \u003Cpath fill=\"#000\" d=\"M96 96h1040v363H96zm310 415h407v69H406z\"\u002F>\n",[221,357,359],{"class":223,"line":358},6,[221,360,361],{},"  \u003Cpath fill=\"#FEEA35\" d=\"M401 506h401v63H401z\"\u002F>\n",[221,363,365],{"class":223,"line":364},7,[221,366,367],{},"  \u003Cpath stroke=\"#000\" stroke-width=\"6\" d=\"M401 506h401v63H401z\"\u002F>\n",[221,369,371],{"class":223,"line":370},8,[221,372,373],{},"  \u003Cpath fill=\"#000\" d=\"M451.415 534.119a4.504 4.504 0 0 0-.588-1.235 3.79 3.79 0 0 0-.887-.946 3.571 3.571 0 0 0-1.176-.589 4.917 4.917 0 0 0-1.449-.204c-.937 0-1.772.236-2.505.707-.733.472-1.31 1.165-1.73 2.08-.415.909-.623 2.017-.623 1.679c.375.733.56 1.622.554 2.668V546h-3.085v-7.858c0-.875-.227-1.56-.682-2.054-.449-.494-1.071-.741-1.866-.741-.54 0-1.02.119-1.441.358a2.477 2.477 0 0 0-.98 1.014c-.233.443-.349.98-.349 1.611Z\"\u002F>\n",[221,375,377],{"class":223,"line":376},9,[221,378,379],{},"  \u003Cpath fill=\"#fff\" d=\"M83 83h1034v357H83z\"\u002F>\n",[221,381,383],{"class":223,"line":382},10,[221,384,385],{},"  \u003Cpath stroke=\"#000\" stroke-width=\"6\" d=\"M83 83h1034v357H83z\"\u002F>\n",[221,387,389],{"class":223,"line":388},11,[221,390,391],{},"  \u003C!-- Text placeholders -->\n",[221,393,395],{"class":223,"line":394},12,[221,396,397],{},"  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"198.318\">{{line1}}\u003C\u002Ftspan>\u003C\u002Ftext>\n",[221,399,401],{"class":223,"line":400},13,[221,402,403],{},"  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"283.318\">{{line2}}\u003C\u002Ftspan>\u003C\u002Ftext>\n",[221,405,407],{"class":223,"line":406},14,[221,408,409],{},"  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"368.318\">{{line3}}\u003C\u002Ftspan>\u003C\u002Ftext>\n",[221,411,413],{"class":223,"line":412},15,[221,414,415],{},"\u003C\u002Fsvg>\n",[129,417,419],{"id":418},"generate-og-image-dynamically","Generate OG image dynamically",[10,421,422,423,426,427,429,430,25],{},"To maintain organization, add a file named ",[38,424,425],{},"genOg.ts"," in the ",[38,428,310],{}," directory. This script contains functions necessary to split the title into three lines and generate the ",[14,431,432],{},"OG image",[10,434,435],{},"First, install the necessary dependencies:",[211,437,441],{"className":438,"code":439,"filename":425,"language":440,"meta":217,"style":217},"language-bash shiki shiki-themes github-light github-light github-dark","pnpm add -D sharp\n","bash",[38,442,443],{"__ignoreMap":217},[221,444,445,449,453,457],{"class":223,"line":224},[221,446,448],{"class":447},"snPdu","pnpm",[221,450,452],{"class":451},"sIIMD"," add",[221,454,456],{"class":455},"sBjJW"," -D",[221,458,459],{"class":451}," sharp\n",[10,461,462],{},"Subsequently, create the function for the OG image generation.",[10,464,465],{},"Steps to accomplish this include:",[94,467,468],{},[97,469,470,471,25],{},"Load the ",[14,472,59],{},[211,474,478],{"className":475,"code":476,"filename":215,"language":477,"meta":217,"style":217},"language-ts shiki shiki-themes github-light github-light github-dark","import { readFileSync } from 'node:fs'\nimport { join } from 'node:path'\n\nconst ogSvg = readFileSync(join('.vitepress', 'og-template.svg'), 'utf-8')\n","ts",[38,479,480,496,508,514],{"__ignoreMap":217},[221,481,482,486,490,493],{"class":223,"line":224},[221,483,485],{"class":484},"s8jYJ","import",[221,487,489],{"class":488},"sxrX7"," { readFileSync } ",[221,491,492],{"class":484},"from",[221,494,495],{"class":451}," 'node:fs'\n",[221,497,498,500,503,505],{"class":223,"line":230},[221,499,485],{"class":484},[221,501,502],{"class":488}," { join } ",[221,504,492],{"class":484},[221,506,507],{"class":451}," 'node:path'\n",[221,509,510],{"class":223,"line":236},[221,511,513],{"emptyLinePlaceholder":512},true,"\n",[221,515,516,519,522,525,528,531,534,536,539,541,544,547,550],{"class":223,"line":346},[221,517,518],{"class":484},"const",[221,520,521],{"class":455}," ogSvg",[221,523,524],{"class":484}," =",[221,526,527],{"class":447}," readFileSync",[221,529,530],{"class":488},"(",[221,532,533],{"class":447},"join",[221,535,530],{"class":488},[221,537,538],{"class":451},"'.vitepress'",[221,540,142],{"class":488},[221,542,543],{"class":451},"'og-template.svg'",[221,545,546],{"class":488},"), ",[221,548,549],{"class":451},"'utf-8'",[221,551,552],{"class":488},")\n",[94,554,555],{"start":230},[97,556,557,558,560],{},"Check for pre-existing ",[14,559,432],{},"; generate if absent:",[211,562,564],{"className":475,"code":563,"filename":215,"language":477,"meta":217,"style":217},"import { existsSync } from 'node:fs'\n\nif (existsSync(output)) {\n  \u002F\u002F eslint-disable-next-line no-useless-return\n  return\n}\n",[38,565,566,577,581,595,601,606],{"__ignoreMap":217},[221,567,568,570,573,575],{"class":223,"line":224},[221,569,485],{"class":484},[221,571,572],{"class":488}," { existsSync } ",[221,574,492],{"class":484},[221,576,495],{"class":451},[221,578,579],{"class":223,"line":230},[221,580,513],{"emptyLinePlaceholder":512},[221,582,583,586,589,592],{"class":223,"line":236},[221,584,585],{"class":484},"if",[221,587,588],{"class":488}," (",[221,590,591],{"class":447},"existsSync",[221,593,594],{"class":488},"(output)) {\n",[221,596,597],{"class":223,"line":346},[221,598,600],{"class":599},"sCsY4","  \u002F\u002F eslint-disable-next-line no-useless-return\n",[221,602,603],{"class":223,"line":352},[221,604,605],{"class":484},"  return\n",[221,607,608],{"class":223,"line":358},[221,609,610],{"class":488},"}\n",[94,612,613],{"start":236},[97,614,615],{},"Create the directory for OG images if it does not exist:",[211,617,619],{"className":475,"code":618,"filename":215,"language":477,"meta":217,"style":217},"import { mkdirSync } from 'node:fs'\nimport { dirname } from 'node:path'\n\nmkdirSync(dirname(output), { recursive: true })\n",[38,620,621,632,643,647],{"__ignoreMap":217},[221,622,623,625,628,630],{"class":223,"line":224},[221,624,485],{"class":484},[221,626,627],{"class":488}," { mkdirSync } ",[221,629,492],{"class":484},[221,631,495],{"class":451},[221,633,634,636,639,641],{"class":223,"line":230},[221,635,485],{"class":484},[221,637,638],{"class":488}," { dirname } ",[221,640,492],{"class":484},[221,642,507],{"class":451},[221,644,645],{"class":223,"line":236},[221,646,513],{"emptyLinePlaceholder":512},[221,648,649,652,654,657,660,663],{"class":223,"line":346},[221,650,651],{"class":447},"mkdirSync",[221,653,530],{"class":488},[221,655,656],{"class":447},"dirname",[221,658,659],{"class":488},"(output), { recursive: ",[221,661,662],{"class":455},"true",[221,664,665],{"class":488}," })\n",[94,667,668],{"start":346},[97,669,670],{},"Break the title into three lines:",[211,672,674],{"className":475,"code":673,"filename":215,"language":477,"meta":217,"style":217},"const lines = title\n  .trim()\n  .split(\u002F(.{0,30})(?:\\s|$)\u002Fg)\n  .filter(Boolean)\n\nconst data: Record\u003Cstring, string> = {\n  line1: lines[0],\n  line2: lines[1],\n  line3: lines[2],\n}\n",[38,675,676,688,699,738,748,752,784,795,805,815],{"__ignoreMap":217},[221,677,678,680,683,685],{"class":223,"line":224},[221,679,518],{"class":484},[221,681,682],{"class":455}," lines",[221,684,524],{"class":484},[221,686,687],{"class":488}," title\n",[221,689,690,693,696],{"class":223,"line":230},[221,691,692],{"class":488},"  .",[221,694,695],{"class":447},"trim",[221,697,698],{"class":488},"()\n",[221,700,701,703,706,708,711,714,716,719,722,725,728,731,733,736],{"class":223,"line":236},[221,702,692],{"class":488},[221,704,705],{"class":447},"split",[221,707,530],{"class":488},[221,709,710],{"class":451},"\u002F",[221,712,530],{"class":713},"s3JiQ",[221,715,25],{"class":455},[221,717,718],{"class":484},"{0,30}",[221,720,721],{"class":713},")(?:",[221,723,724],{"class":455},"\\s",[221,726,727],{"class":484},"|$",[221,729,730],{"class":713},")",[221,732,710],{"class":451},[221,734,735],{"class":484},"g",[221,737,552],{"class":488},[221,739,740,742,745],{"class":223,"line":346},[221,741,692],{"class":488},[221,743,744],{"class":447},"filter",[221,746,747],{"class":488},"(Boolean)\n",[221,749,750],{"class":223,"line":352},[221,751,513],{"emptyLinePlaceholder":512},[221,753,754,756,759,762,765,768,771,773,775,778,781],{"class":223,"line":358},[221,755,518],{"class":484},[221,757,758],{"class":455}," data",[221,760,761],{"class":484},":",[221,763,764],{"class":447}," Record",[221,766,767],{"class":488},"\u003C",[221,769,770],{"class":455},"string",[221,772,142],{"class":488},[221,774,770],{"class":455},[221,776,777],{"class":488},"> ",[221,779,780],{"class":484},"=",[221,782,783],{"class":488}," {\n",[221,785,786,789,792],{"class":223,"line":364},[221,787,788],{"class":488},"  line1: lines[",[221,790,791],{"class":455},"0",[221,793,794],{"class":488},"],\n",[221,796,797,800,803],{"class":223,"line":370},[221,798,799],{"class":488},"  line2: lines[",[221,801,802],{"class":455},"1",[221,804,794],{"class":488},[221,806,807,810,813],{"class":223,"line":376},[221,808,809],{"class":488},"  line3: lines[",[221,811,812],{"class":455},"2",[221,814,794],{"class":488},[221,816,817],{"class":223,"line":382},[221,818,610],{"class":488},[94,820,821],{"start":352},[97,822,823],{},"Substitute placeholders in the SVG template with title lines:",[211,825,827],{"className":475,"code":826,"filename":215,"language":477,"meta":217,"style":217},"const svg = ogSvg.replace(\u002F\\{\\{([^}]+)\\}\\}\u002Fg, (_, name) => data[name] || '')\n",[38,828,829],{"__ignoreMap":217},[221,830,831,833,836,838,841,844,846,848,852,854,857,860,863,866,868,871,873,875,878,882,884,887,890,893,896,899,902],{"class":223,"line":224},[221,832,518],{"class":484},[221,834,835],{"class":455}," svg",[221,837,524],{"class":484},[221,839,840],{"class":488}," ogSvg.",[221,842,843],{"class":447},"replace",[221,845,530],{"class":488},[221,847,710],{"class":451},[221,849,851],{"class":850},"sujWL","\\{\\{",[221,853,530],{"class":713},[221,855,856],{"class":455},"[",[221,858,859],{"class":484},"^",[221,861,862],{"class":455},"}]",[221,864,865],{"class":484},"+",[221,867,730],{"class":713},[221,869,870],{"class":850},"\\}\\}",[221,872,710],{"class":451},[221,874,735],{"class":484},[221,876,877],{"class":488},", (",[221,879,881],{"class":880},"sP4rz","_",[221,883,142],{"class":488},[221,885,886],{"class":880},"name",[221,888,889],{"class":488},") ",[221,891,892],{"class":484},"=>",[221,894,895],{"class":488}," data[name] ",[221,897,898],{"class":484},"||",[221,900,901],{"class":451}," ''",[221,903,552],{"class":488},[94,905,906],{"start":358},[97,907,908,909,63,911,913],{},"Transform the ",[14,910,59],{},[14,912,66],{}," and store it:",[211,915,917],{"className":475,"code":916,"filename":215,"language":477,"meta":217,"style":217},"import { Buffer } from 'node:buffer'\nimport sharp from 'sharp'\n\nawait sharp(Buffer.from(svg)).resize(1440, 810).png().toFile(output)\n",[38,918,919,931,943,947],{"__ignoreMap":217},[221,920,921,923,926,928],{"class":223,"line":224},[221,922,485],{"class":484},[221,924,925],{"class":488}," { Buffer } ",[221,927,492],{"class":484},[221,929,930],{"class":451}," 'node:buffer'\n",[221,932,933,935,938,940],{"class":223,"line":230},[221,934,485],{"class":484},[221,936,937],{"class":488}," sharp ",[221,939,492],{"class":484},[221,941,942],{"class":451}," 'sharp'\n",[221,944,945],{"class":223,"line":236},[221,946,513],{"emptyLinePlaceholder":512},[221,948,949,952,955,958,960,963,966,968,971,973,976,979,982,985,988],{"class":223,"line":346},[221,950,951],{"class":484},"await",[221,953,954],{"class":447}," sharp",[221,956,957],{"class":488},"(Buffer.",[221,959,492],{"class":447},[221,961,962],{"class":488},"(svg)).",[221,964,965],{"class":447},"resize",[221,967,530],{"class":488},[221,969,970],{"class":455},"1440",[221,972,142],{"class":488},[221,974,975],{"class":455},"810",[221,977,978],{"class":488},").",[221,980,981],{"class":447},"png",[221,983,984],{"class":488},"().",[221,986,987],{"class":447},"toFile",[221,989,990],{"class":488},"(output)\n",[10,992,993],{},"Here is the comprehensive function:",[211,995,997],{"className":475,"code":996,"filename":215,"language":477,"meta":217,"style":217},"import { Buffer } from 'node:buffer'\nimport { existsSync, mkdirSync, readFileSync } from 'node:fs'\nimport { dirname, join } from 'node:path'\nimport sharp from 'sharp'\n\nconst ogSvg: string = readFileSync(\n  join('.vitepress', 'og-template.svg'),\n  'utf-8',\n)\n\n\u002F**\n * @credit Anthony Fu, https:\u002F\u002Fantfu.me\n * @link https:\u002F\u002Fgithub.com\u002Fantfu\u002Fantfu.me\u002Fblob\u002Fmain\u002Fvite.config.ts#L242\n *\u002F\nexport async function genOg(title: string, output: string) {\n  \u002F\u002F Skip if the file already exists\n  if (existsSync(output))\n    return\n\n  \u002F\u002F Ensure the output directory exists\n  mkdirSync(dirname(output), { recursive: true })\n\n  \u002F\u002F Break the title into lines of 30 characters\n  const lines = title\n    .trim()\n    .split(\u002F(.{0,30})(?:\\s|$)\u002Fg)\n    .filter(Boolean)\n\n  const data: Record\u003Cstring, string> = {\n    line1: lines[0],\n    line2: lines[1],\n    line3: lines[2],\n  }\n  const svg = ogSvg.replace(\u002F\\{\\{([^}]+)\\}\\}\u002Fg, (_, name) => data[name] || '')\n\n  console.info(`Generating ${output}`)\n  try {\n    await sharp(Buffer.from(svg)).resize(1440, 810).png().toFile(output)\n  }\n  catch (e) {\n    console.error('Failed to generate og image', e)\n  }\n}\n",[38,998,999,1009,1020,1031,1041,1045,1063,1079,1087,1091,1095,1100,1111,1121,1126,1161,1167,1180,1186,1191,1197,1213,1218,1224,1236,1246,1277,1286,1291,1316,1326,1336,1346,1352,1409,1414,1434,1442,1476,1481,1490,1507,1512],{"__ignoreMap":217},[221,1000,1001,1003,1005,1007],{"class":223,"line":224},[221,1002,485],{"class":484},[221,1004,925],{"class":488},[221,1006,492],{"class":484},[221,1008,930],{"class":451},[221,1010,1011,1013,1016,1018],{"class":223,"line":230},[221,1012,485],{"class":484},[221,1014,1015],{"class":488}," { existsSync, mkdirSync, readFileSync } ",[221,1017,492],{"class":484},[221,1019,495],{"class":451},[221,1021,1022,1024,1027,1029],{"class":223,"line":236},[221,1023,485],{"class":484},[221,1025,1026],{"class":488}," { dirname, join } ",[221,1028,492],{"class":484},[221,1030,507],{"class":451},[221,1032,1033,1035,1037,1039],{"class":223,"line":346},[221,1034,485],{"class":484},[221,1036,937],{"class":488},[221,1038,492],{"class":484},[221,1040,942],{"class":451},[221,1042,1043],{"class":223,"line":352},[221,1044,513],{"emptyLinePlaceholder":512},[221,1046,1047,1049,1051,1053,1056,1058,1060],{"class":223,"line":358},[221,1048,518],{"class":484},[221,1050,521],{"class":455},[221,1052,761],{"class":484},[221,1054,1055],{"class":455}," string",[221,1057,524],{"class":484},[221,1059,527],{"class":447},[221,1061,1062],{"class":488},"(\n",[221,1064,1065,1068,1070,1072,1074,1076],{"class":223,"line":364},[221,1066,1067],{"class":447},"  join",[221,1069,530],{"class":488},[221,1071,538],{"class":451},[221,1073,142],{"class":488},[221,1075,543],{"class":451},[221,1077,1078],{"class":488},"),\n",[221,1080,1081,1084],{"class":223,"line":370},[221,1082,1083],{"class":451},"  'utf-8'",[221,1085,1086],{"class":488},",\n",[221,1088,1089],{"class":223,"line":376},[221,1090,552],{"class":488},[221,1092,1093],{"class":223,"line":382},[221,1094,513],{"emptyLinePlaceholder":512},[221,1096,1097],{"class":223,"line":388},[221,1098,1099],{"class":599},"\u002F**\n",[221,1101,1102,1105,1108],{"class":223,"line":394},[221,1103,1104],{"class":599}," * ",[221,1106,1107],{"class":484},"@credit",[221,1109,1110],{"class":599}," Anthony Fu, https:\u002F\u002Fantfu.me\n",[221,1112,1113,1115,1118],{"class":223,"line":400},[221,1114,1104],{"class":599},[221,1116,1117],{"class":484},"@link",[221,1119,1120],{"class":599}," https:\u002F\u002Fgithub.com\u002Fantfu\u002Fantfu.me\u002Fblob\u002Fmain\u002Fvite.config.ts#L242\n",[221,1122,1123],{"class":223,"line":406},[221,1124,1125],{"class":599}," *\u002F\n",[221,1127,1128,1131,1134,1137,1140,1142,1145,1147,1149,1151,1154,1156,1158],{"class":223,"line":412},[221,1129,1130],{"class":484},"export",[221,1132,1133],{"class":484}," async",[221,1135,1136],{"class":484}," function",[221,1138,1139],{"class":447}," genOg",[221,1141,530],{"class":488},[221,1143,1144],{"class":880},"title",[221,1146,761],{"class":484},[221,1148,1055],{"class":455},[221,1150,142],{"class":488},[221,1152,1153],{"class":880},"output",[221,1155,761],{"class":484},[221,1157,1055],{"class":455},[221,1159,1160],{"class":488},") {\n",[221,1162,1164],{"class":223,"line":1163},16,[221,1165,1166],{"class":599},"  \u002F\u002F Skip if the file already exists\n",[221,1168,1170,1173,1175,1177],{"class":223,"line":1169},17,[221,1171,1172],{"class":484},"  if",[221,1174,588],{"class":488},[221,1176,591],{"class":447},[221,1178,1179],{"class":488},"(output))\n",[221,1181,1183],{"class":223,"line":1182},18,[221,1184,1185],{"class":484},"    return\n",[221,1187,1189],{"class":223,"line":1188},19,[221,1190,513],{"emptyLinePlaceholder":512},[221,1192,1194],{"class":223,"line":1193},20,[221,1195,1196],{"class":599},"  \u002F\u002F Ensure the output directory exists\n",[221,1198,1200,1203,1205,1207,1209,1211],{"class":223,"line":1199},21,[221,1201,1202],{"class":447},"  mkdirSync",[221,1204,530],{"class":488},[221,1206,656],{"class":447},[221,1208,659],{"class":488},[221,1210,662],{"class":455},[221,1212,665],{"class":488},[221,1214,1216],{"class":223,"line":1215},22,[221,1217,513],{"emptyLinePlaceholder":512},[221,1219,1221],{"class":223,"line":1220},23,[221,1222,1223],{"class":599},"  \u002F\u002F Break the title into lines of 30 characters\n",[221,1225,1227,1230,1232,1234],{"class":223,"line":1226},24,[221,1228,1229],{"class":484},"  const",[221,1231,682],{"class":455},[221,1233,524],{"class":484},[221,1235,687],{"class":488},[221,1237,1239,1242,1244],{"class":223,"line":1238},25,[221,1240,1241],{"class":488},"    .",[221,1243,695],{"class":447},[221,1245,698],{"class":488},[221,1247,1249,1251,1253,1255,1257,1259,1261,1263,1265,1267,1269,1271,1273,1275],{"class":223,"line":1248},26,[221,1250,1241],{"class":488},[221,1252,705],{"class":447},[221,1254,530],{"class":488},[221,1256,710],{"class":451},[221,1258,530],{"class":713},[221,1260,25],{"class":455},[221,1262,718],{"class":484},[221,1264,721],{"class":713},[221,1266,724],{"class":455},[221,1268,727],{"class":484},[221,1270,730],{"class":713},[221,1272,710],{"class":451},[221,1274,735],{"class":484},[221,1276,552],{"class":488},[221,1278,1280,1282,1284],{"class":223,"line":1279},27,[221,1281,1241],{"class":488},[221,1283,744],{"class":447},[221,1285,747],{"class":488},[221,1287,1289],{"class":223,"line":1288},28,[221,1290,513],{"emptyLinePlaceholder":512},[221,1292,1294,1296,1298,1300,1302,1304,1306,1308,1310,1312,1314],{"class":223,"line":1293},29,[221,1295,1229],{"class":484},[221,1297,758],{"class":455},[221,1299,761],{"class":484},[221,1301,764],{"class":447},[221,1303,767],{"class":488},[221,1305,770],{"class":455},[221,1307,142],{"class":488},[221,1309,770],{"class":455},[221,1311,777],{"class":488},[221,1313,780],{"class":484},[221,1315,783],{"class":488},[221,1317,1319,1322,1324],{"class":223,"line":1318},30,[221,1320,1321],{"class":488},"    line1: lines[",[221,1323,791],{"class":455},[221,1325,794],{"class":488},[221,1327,1329,1332,1334],{"class":223,"line":1328},31,[221,1330,1331],{"class":488},"    line2: lines[",[221,1333,802],{"class":455},[221,1335,794],{"class":488},[221,1337,1339,1342,1344],{"class":223,"line":1338},32,[221,1340,1341],{"class":488},"    line3: lines[",[221,1343,812],{"class":455},[221,1345,794],{"class":488},[221,1347,1349],{"class":223,"line":1348},33,[221,1350,1351],{"class":488},"  }\n",[221,1353,1355,1357,1359,1361,1363,1365,1367,1369,1371,1373,1375,1377,1379,1381,1383,1385,1387,1389,1391,1393,1395,1397,1399,1401,1403,1405,1407],{"class":223,"line":1354},34,[221,1356,1229],{"class":484},[221,1358,835],{"class":455},[221,1360,524],{"class":484},[221,1362,840],{"class":488},[221,1364,843],{"class":447},[221,1366,530],{"class":488},[221,1368,710],{"class":451},[221,1370,851],{"class":850},[221,1372,530],{"class":713},[221,1374,856],{"class":455},[221,1376,859],{"class":484},[221,1378,862],{"class":455},[221,1380,865],{"class":484},[221,1382,730],{"class":713},[221,1384,870],{"class":850},[221,1386,710],{"class":451},[221,1388,735],{"class":484},[221,1390,877],{"class":488},[221,1392,881],{"class":880},[221,1394,142],{"class":488},[221,1396,886],{"class":880},[221,1398,889],{"class":488},[221,1400,892],{"class":484},[221,1402,895],{"class":488},[221,1404,898],{"class":484},[221,1406,901],{"class":451},[221,1408,552],{"class":488},[221,1410,1412],{"class":223,"line":1411},35,[221,1413,513],{"emptyLinePlaceholder":512},[221,1415,1417,1420,1422,1424,1427,1429,1432],{"class":223,"line":1416},36,[221,1418,1419],{"class":488},"  console.",[221,1421,29],{"class":447},[221,1423,530],{"class":488},[221,1425,1426],{"class":451},"`Generating ${",[221,1428,1153],{"class":488},[221,1430,1431],{"class":451},"}`",[221,1433,552],{"class":488},[221,1435,1437,1440],{"class":223,"line":1436},37,[221,1438,1439],{"class":484},"  try",[221,1441,783],{"class":488},[221,1443,1445,1448,1450,1452,1454,1456,1458,1460,1462,1464,1466,1468,1470,1472,1474],{"class":223,"line":1444},38,[221,1446,1447],{"class":484},"    await",[221,1449,954],{"class":447},[221,1451,957],{"class":488},[221,1453,492],{"class":447},[221,1455,962],{"class":488},[221,1457,965],{"class":447},[221,1459,530],{"class":488},[221,1461,970],{"class":455},[221,1463,142],{"class":488},[221,1465,975],{"class":455},[221,1467,978],{"class":488},[221,1469,981],{"class":447},[221,1471,984],{"class":488},[221,1473,987],{"class":447},[221,1475,990],{"class":488},[221,1477,1479],{"class":223,"line":1478},39,[221,1480,1351],{"class":488},[221,1482,1484,1487],{"class":223,"line":1483},40,[221,1485,1486],{"class":484},"  catch",[221,1488,1489],{"class":488}," (e) {\n",[221,1491,1493,1496,1499,1501,1504],{"class":223,"line":1492},41,[221,1494,1495],{"class":488},"    console.",[221,1497,1498],{"class":447},"error",[221,1500,530],{"class":488},[221,1502,1503],{"class":451},"'Failed to generate og image'",[221,1505,1506],{"class":488},", e)\n",[221,1508,1510],{"class":223,"line":1509},42,[221,1511,1351],{"class":488},[221,1513,1515],{"class":223,"line":1514},43,[221,1516,610],{"class":488},[129,1518,1520],{"id":1519},"assign-og-images-to-each-post","Assign OG images to each post",[10,1522,1523],{},"Now that we have a functional method to generate OG images utilizing a title, it is crucial to determine when and where this function should be invoked.",[10,1525,1526,1527,1529,1530,1533],{},"The easiest strategy is to operate this function every time a file is processed by ",[14,1528,119],{}," within the ",[38,1531,1532],{},"transformPageData",", subsequent to the adjustments from the previous article.",[10,1535,1536],{},"Initially, extract the name for the OG image using the slugged file path:",[211,1538,1540],{"className":475,"code":1539,"filename":215,"language":477,"meta":217,"style":217},"import { defineConfig } from 'vitepress'\n\nexport default defineConfig({\n  async transformPageData(pageData) {\n    \u002F\u002F ...\n\n    const ogName = pageData.filePath\n      .replaceAll(\u002F\\\u002F\u002Fg, '-')\n      .replace(\u002F\\.md$\u002F, '.png')\n  }\n})\n",[38,1541,1542,1554,1558,1571,1586,1591,1595,1608,1634,1662,1666],{"__ignoreMap":217},[221,1543,1544,1546,1549,1551],{"class":223,"line":224},[221,1545,485],{"class":484},[221,1547,1548],{"class":488}," { defineConfig } ",[221,1550,492],{"class":484},[221,1552,1553],{"class":451}," 'vitepress'\n",[221,1555,1556],{"class":223,"line":230},[221,1557,513],{"emptyLinePlaceholder":512},[221,1559,1560,1562,1565,1568],{"class":223,"line":236},[221,1561,1130],{"class":484},[221,1563,1564],{"class":484}," default",[221,1566,1567],{"class":447}," defineConfig",[221,1569,1570],{"class":488},"({\n",[221,1572,1573,1576,1579,1581,1584],{"class":223,"line":346},[221,1574,1575],{"class":484},"  async",[221,1577,1578],{"class":447}," transformPageData",[221,1580,530],{"class":488},[221,1582,1583],{"class":880},"pageData",[221,1585,1160],{"class":488},[221,1587,1588],{"class":223,"line":352},[221,1589,1590],{"class":599},"    \u002F\u002F ...\n",[221,1592,1593],{"class":223,"line":358},[221,1594,513],{"emptyLinePlaceholder":512},[221,1596,1597,1600,1603,1605],{"class":223,"line":364},[221,1598,1599],{"class":484},"    const",[221,1601,1602],{"class":455}," ogName",[221,1604,524],{"class":484},[221,1606,1607],{"class":488}," pageData.filePath\n",[221,1609,1610,1613,1616,1618,1620,1623,1625,1627,1629,1632],{"class":223,"line":370},[221,1611,1612],{"class":488},"      .",[221,1614,1615],{"class":447},"replaceAll",[221,1617,530],{"class":488},[221,1619,710],{"class":451},[221,1621,1622],{"class":850},"\\\u002F",[221,1624,710],{"class":451},[221,1626,735],{"class":484},[221,1628,142],{"class":488},[221,1630,1631],{"class":451},"'-'",[221,1633,552],{"class":488},[221,1635,1636,1638,1640,1642,1644,1647,1650,1653,1655,1657,1660],{"class":223,"line":376},[221,1637,1612],{"class":488},[221,1639,843],{"class":447},[221,1641,530],{"class":488},[221,1643,710],{"class":451},[221,1645,1646],{"class":850},"\\.",[221,1648,1649],{"class":713},"md",[221,1651,1652],{"class":484},"$",[221,1654,710],{"class":451},[221,1656,142],{"class":488},[221,1658,1659],{"class":451},"'.png'",[221,1661,552],{"class":488},[221,1663,1664],{"class":223,"line":382},[221,1665,1351],{"class":488},[221,1667,1668],{"class":223,"line":388},[221,1669,1670],{"class":488},"})\n",[10,1672,1673,1674,1677,1678,1681],{},"Here, a file path such as ",[38,1675,1676],{},"blog\u002Flearn-vitepress.md"," transforms into ",[38,1679,1680],{},"blog-learn-vitepress.png",". This transformation is safe because Markdown filenames are already URL-compatible and adhere to filesystem norms.",[10,1683,1684],{},"Next, invoke the function:",[211,1686,1688],{"className":475,"code":1687,"filename":1676,"language":477,"meta":217,"style":217},"import { joinURL } from 'ufo'\nimport { defineConfig } from 'vitepress'\nimport { genOg } from '.\u002FgenOg'\n\nexport default defineConfig({\n  async transformPageData(pageData, { siteConfig }) { \u002F\u002F Be careful, `{ siteConfig }` is a new parameter\n    \u002F\u002F ...\n\n    await genOg(\n      pageData.frontmatter.title || pageData.title || siteConfig.site.title,\n      joinURL(siteConfig.srcDir, 'public', 'og', ogName),\n    )\n  }\n})\n",[38,1689,1690,1702,1712,1724,1728,1738,1760,1764,1768,1776,1791,1810,1815,1819],{"__ignoreMap":217},[221,1691,1692,1694,1697,1699],{"class":223,"line":224},[221,1693,485],{"class":484},[221,1695,1696],{"class":488}," { joinURL } ",[221,1698,492],{"class":484},[221,1700,1701],{"class":451}," 'ufo'\n",[221,1703,1704,1706,1708,1710],{"class":223,"line":230},[221,1705,485],{"class":484},[221,1707,1548],{"class":488},[221,1709,492],{"class":484},[221,1711,1553],{"class":451},[221,1713,1714,1716,1719,1721],{"class":223,"line":236},[221,1715,485],{"class":484},[221,1717,1718],{"class":488}," { genOg } ",[221,1720,492],{"class":484},[221,1722,1723],{"class":451}," '.\u002FgenOg'\n",[221,1725,1726],{"class":223,"line":346},[221,1727,513],{"emptyLinePlaceholder":512},[221,1729,1730,1732,1734,1736],{"class":223,"line":352},[221,1731,1130],{"class":484},[221,1733,1564],{"class":484},[221,1735,1567],{"class":447},[221,1737,1570],{"class":488},[221,1739,1740,1742,1744,1746,1748,1751,1754,1757],{"class":223,"line":358},[221,1741,1575],{"class":484},[221,1743,1578],{"class":447},[221,1745,530],{"class":488},[221,1747,1583],{"class":880},[221,1749,1750],{"class":488},", { ",[221,1752,1753],{"class":880},"siteConfig",[221,1755,1756],{"class":488}," }) { ",[221,1758,1759],{"class":599},"\u002F\u002F Be careful, `{ siteConfig }` is a new parameter\n",[221,1761,1762],{"class":223,"line":364},[221,1763,1590],{"class":599},[221,1765,1766],{"class":223,"line":370},[221,1767,513],{"emptyLinePlaceholder":512},[221,1769,1770,1772,1774],{"class":223,"line":376},[221,1771,1447],{"class":484},[221,1773,1139],{"class":447},[221,1775,1062],{"class":488},[221,1777,1778,1781,1783,1786,1788],{"class":223,"line":382},[221,1779,1780],{"class":488},"      pageData.frontmatter.title ",[221,1782,898],{"class":484},[221,1784,1785],{"class":488}," pageData.title ",[221,1787,898],{"class":484},[221,1789,1790],{"class":488}," siteConfig.site.title,\n",[221,1792,1793,1796,1799,1802,1804,1807],{"class":223,"line":388},[221,1794,1795],{"class":447},"      joinURL",[221,1797,1798],{"class":488},"(siteConfig.srcDir, ",[221,1800,1801],{"class":451},"'public'",[221,1803,142],{"class":488},[221,1805,1806],{"class":451},"'og'",[221,1808,1809],{"class":488},", ogName),\n",[221,1811,1812],{"class":223,"line":394},[221,1813,1814],{"class":488},"    )\n",[221,1816,1817],{"class":223,"line":400},[221,1818,1351],{"class":488},[221,1820,1821],{"class":223,"line":406},[221,1822,1670],{"class":488},[10,1824,1825,1826,1828,1829,1832,1833,1836,1837,25],{},"To ensure a fallback approach, we check for these potential titles: the frontmatter title, the inferred page title from the first h1, or, if neither are present, use the site title. This prevents the output of a blank ",[14,1827,432],{},". Store generated OG images in the ",[38,1830,1831],{},"public"," directory under ",[38,1834,1835],{},"og"," to avoid name conflicts, as this folder merges into the ",[38,1838,1839],{},"dist",[10,1841,1842,1843,426,1845,1847],{},"Include ",[38,1844,1835],{},[38,1846,73],{}," file since it only contains generated outputs.",[129,1849,1851],{"id":1850},"incorporate-metadata","Incorporate metadata",[10,1853,1854,1855,1858,1859,1862,1863,1866],{},"Lastly, append ",[14,1856,1857],{},"metadata"," to inform social networks about the ",[14,1860,1861],{},"OG image's"," location, leveraging the previous ",[14,1864,1865],{},"SEO"," article advancements.",[211,1868,1870],{"className":475,"code":1869,"filename":215,"language":477,"meta":217,"style":217},"import { joinURL } from 'ufo'\nimport { defineConfig } from 'vitepress'\n\nexport default defineConfig({\n  async transformPageData(pageData, { siteConfig }) {\n    \u002F\u002F ...\n\n    \u002F\u002F Integrate OG image URL into frontmatter\n    pageData.frontmatter.head.push(\n      [\n        'meta',\n        {\n          property: 'og:image',\n          content: joinURL(\n            'https:\u002F\u002Fgarabit.barbapapazes.dev', \u002F\u002F Please, change this before deploying\n            'og',\n            ogName,\n          ),\n        },\n      ],\n      [\n        'meta',\n        {\n          name: 'twitter:image',\n          content: joinURL(\n            'https:\u002F\u002Fgarabit.barbapapazes.dev', \u002F\u002F Please, change this before deploying\n            'og',\n            ogName,\n          ),\n        },\n      ],\n    )\n  }\n})\n",[38,1871,1872,1882,1892,1896,1906,1923,1927,1931,1936,1946,1951,1958,1963,1973,1983,1993,2000,2005,2010,2015,2020,2024,2030,2034,2044,2052,2060,2066,2070,2074,2078,2082,2086,2090],{"__ignoreMap":217},[221,1873,1874,1876,1878,1880],{"class":223,"line":224},[221,1875,485],{"class":484},[221,1877,1696],{"class":488},[221,1879,492],{"class":484},[221,1881,1701],{"class":451},[221,1883,1884,1886,1888,1890],{"class":223,"line":230},[221,1885,485],{"class":484},[221,1887,1548],{"class":488},[221,1889,492],{"class":484},[221,1891,1553],{"class":451},[221,1893,1894],{"class":223,"line":236},[221,1895,513],{"emptyLinePlaceholder":512},[221,1897,1898,1900,1902,1904],{"class":223,"line":346},[221,1899,1130],{"class":484},[221,1901,1564],{"class":484},[221,1903,1567],{"class":447},[221,1905,1570],{"class":488},[221,1907,1908,1910,1912,1914,1916,1918,1920],{"class":223,"line":352},[221,1909,1575],{"class":484},[221,1911,1578],{"class":447},[221,1913,530],{"class":488},[221,1915,1583],{"class":880},[221,1917,1750],{"class":488},[221,1919,1753],{"class":880},[221,1921,1922],{"class":488}," }) {\n",[221,1924,1925],{"class":223,"line":358},[221,1926,1590],{"class":599},[221,1928,1929],{"class":223,"line":364},[221,1930,513],{"emptyLinePlaceholder":512},[221,1932,1933],{"class":223,"line":370},[221,1934,1935],{"class":599},"    \u002F\u002F Integrate OG image URL into frontmatter\n",[221,1937,1938,1941,1944],{"class":223,"line":376},[221,1939,1940],{"class":488},"    pageData.frontmatter.head.",[221,1942,1943],{"class":447},"push",[221,1945,1062],{"class":488},[221,1947,1948],{"class":223,"line":382},[221,1949,1950],{"class":488},"      [\n",[221,1952,1953,1956],{"class":223,"line":388},[221,1954,1955],{"class":451},"        'meta'",[221,1957,1086],{"class":488},[221,1959,1960],{"class":223,"line":394},[221,1961,1962],{"class":488},"        {\n",[221,1964,1965,1968,1971],{"class":223,"line":400},[221,1966,1967],{"class":488},"          property: ",[221,1969,1970],{"class":451},"'og:image'",[221,1972,1086],{"class":488},[221,1974,1975,1978,1981],{"class":223,"line":406},[221,1976,1977],{"class":488},"          content: ",[221,1979,1980],{"class":447},"joinURL",[221,1982,1062],{"class":488},[221,1984,1985,1988,1990],{"class":223,"line":412},[221,1986,1987],{"class":451},"            'https:\u002F\u002Fgarabit.barbapapazes.dev'",[221,1989,142],{"class":488},[221,1991,1992],{"class":599},"\u002F\u002F Please, change this before deploying\n",[221,1994,1995,1998],{"class":223,"line":1163},[221,1996,1997],{"class":451},"            'og'",[221,1999,1086],{"class":488},[221,2001,2002],{"class":223,"line":1169},[221,2003,2004],{"class":488},"            ogName,\n",[221,2006,2007],{"class":223,"line":1182},[221,2008,2009],{"class":488},"          ),\n",[221,2011,2012],{"class":223,"line":1188},[221,2013,2014],{"class":488},"        },\n",[221,2016,2017],{"class":223,"line":1193},[221,2018,2019],{"class":488},"      ],\n",[221,2021,2022],{"class":223,"line":1199},[221,2023,1950],{"class":488},[221,2025,2026,2028],{"class":223,"line":1215},[221,2027,1955],{"class":451},[221,2029,1086],{"class":488},[221,2031,2032],{"class":223,"line":1220},[221,2033,1962],{"class":488},[221,2035,2036,2039,2042],{"class":223,"line":1226},[221,2037,2038],{"class":488},"          name: ",[221,2040,2041],{"class":451},"'twitter:image'",[221,2043,1086],{"class":488},[221,2045,2046,2048,2050],{"class":223,"line":1238},[221,2047,1977],{"class":488},[221,2049,1980],{"class":447},[221,2051,1062],{"class":488},[221,2053,2054,2056,2058],{"class":223,"line":1248},[221,2055,1987],{"class":451},[221,2057,142],{"class":488},[221,2059,1992],{"class":599},[221,2061,2062,2064],{"class":223,"line":1279},[221,2063,1997],{"class":451},[221,2065,1086],{"class":488},[221,2067,2068],{"class":223,"line":1288},[221,2069,2004],{"class":488},[221,2071,2072],{"class":223,"line":1293},[221,2073,2009],{"class":488},[221,2075,2076],{"class":223,"line":1318},[221,2077,2014],{"class":488},[221,2079,2080],{"class":223,"line":1328},[221,2081,2019],{"class":488},[221,2083,2084],{"class":223,"line":1338},[221,2085,1814],{"class":488},[221,2087,2088],{"class":223,"line":1348},[221,2089,1351],{"class":488},[221,2091,2092],{"class":223,"line":1354},[221,2093,1670],{"class":488},[50,2095,2097],{"id":2096},"an-og-image-for-every-path","An OG image for every path",[10,2099,2100,2101,2104,2105,2107],{},"This guide was anticipated eagerly as ",[14,2102,2103],{},"OG images"," are both pivotal and laborious to produce. The mechanisms here, which I have employed for several months, ensure the swift regeneration of ",[14,2106,2103],{}," upon template changes. Over time, I have continuously tailored the template to align with evolving needs. Expanding on this methodology, I recently incorporated a headline for series titles following similar techniques.",[10,2109,2110],{},"I can only recommend this simple approach, as it has significantly improved my workflow. I hope it will do the same for you.",[156,2112,2113,2117],{},[159,2114],{"alt":2115,"src":2116},"OG Image from this Article","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post\u002Farticle-og.png",[164,2118,2115],{},[2120,2121,2122],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .snPdu, html code.shiki .snPdu{--shiki-light:#6F42C1;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sIIMD, html code.shiki .sIIMD{--shiki-light:#032F62;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sBjJW, html code.shiki .sBjJW{--shiki-light:#005CC5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s8jYJ, html code.shiki .s8jYJ{--shiki-light:#D73A49;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sxrX7, html code.shiki .sxrX7{--shiki-light:#24292E;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sCsY4, html code.shiki .sCsY4{--shiki-light:#6A737D;--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .s3JiQ, html code.shiki .s3JiQ{--shiki-light:#032F62;--shiki-default:#032F62;--shiki-dark:#DBEDFF}html pre.shiki code .sujWL, html code.shiki .sujWL{--shiki-light:#22863A;--shiki-light-font-weight:bold;--shiki-default:#22863A;--shiki-default-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold}html pre.shiki code .sP4rz, html code.shiki .sP4rz{--shiki-light:#E36209;--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":217,"searchDepth":230,"depth":230,"links":2124},[2125,2126,2133],{"id":52,"depth":230,"text":53},{"id":123,"depth":230,"text":124,"children":2127},[2128,2129,2130,2131,2132],{"id":131,"depth":236,"text":132},{"id":186,"depth":236,"text":187},{"id":418,"depth":236,"text":419},{"id":1519,"depth":236,"text":1520},{"id":1850,"depth":236,"text":1851},{"id":2096,"depth":230,"text":2097},"c76a349e-a495-4890-ab05-3e8b6bb90add","2024-11-05","Learn how to dynamically generate Open Graph images for each blog post, enhancing the visual appeal when sharing content on social media.",{"branch":2138},"08.automatically-generated-open-graph-images",null,{},"\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post","---\ncontentId: c76a349e-a495-4890-ab05-3e8b6bb90add\npart: 8\ntitle: Creating Open Graph Images Automatically for Each Blog Post\ndescription: Learn how to dynamically generate Open Graph images for each blog post, enhancing the visual appeal when sharing content on social media.\ngithub:\n  branch: 08.automatically-generated-open-graph-images\ndate: 2024-11-05\nseriesSlug: create-a-blog-with-vitepress-and-vue-js-from-scratch\n---\n\nSharing content on **social media** is one of the most effective strategies to engage with a wider audience. When promoting a blog post, including an **Open Graph image (OG image)** is crucial for improving the post's visual appeal. These images are displayed when sharing links on platforms such as **Twitter, Facebook, and LinkedIn**.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\n**An OG image is a PNG or JPEG** that should be at least 1200 x 630 pixels in size, less than 5MB, and referenced in the `\u003Cmeta property=\"og:image\" content=\"...\">` tag within the HTML `\u003Chead>` section using an absolute URL.\n::\n\nCreating an OG image for each blog post manually can be time-consuming. Moreover, any design modifications necessitate updating all the OG images, which is impractical in the long run. It's essential to be agile in updating and evolving our blog efficiently without dedicating excessive time. Keep in mind our goal:  Writing is the primary focus, the rest is automated.\n\n## The theory\n\nTo dynamically generate an OG image, we first need to create an **SVG template**. In this template, placeholders will represent content like the page title, description, and author. Using a suitable library, we will convert this **SVG template** into a **PNG image** for each page, utilizing the designated placeholders.\n\nThese generated images will be added to the `.gitignore` file, ensuring that each time we generate the website, the OG images are also created. During development, we check if an OG image already exists to avoid unnecessary computation.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThis approach was inspired by [**Anthony Fu's blog**](https:\u002F\u002Fgithub.com\u002Fantfu\u002Fantfu.me\u002Fblob\u002Fmain\u002Fvite.config.ts#L242). I have adapted it to suit our requirements.\n::\n\nThe steps for generating the OG images are as follows:\n\n1. Create an SVG template with **Figma** (or another tool of choice).\n2. Export two versions: one with outlined text and one without.\n3. Manually integrate the two SVG templates, preserving the outlined text while keeping the placeholders and `\u003Ctext>` tags from the original.\n4. Implement a function in **VitePress** to dynamically create the OG image.\n\n## The implementation\n\nEnough theory; let's delve into the implementation.\n\n### Create the SVG template\n\nIn **Figma**, design an OG image template with dimensions of 1200 x 630 pixels. Define placeholders for the post title using `{{line1}}`, `{{line2}}`, and `{{line3}}`, each accommodating up to 30 characters, allowing a total of 90 characters for most titles. Remember, **SEO practices** discourage titles exceeding 100 characters.\n\n![OG Image Template in Figma](\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post\u002Fvitepress-figma-og.png)\n\n::callout{icon=\"i-ph-lightbulb-duotone\" color=\"success\"}\nIf you're uncertain about getting started, consider using [the template I created for this project](https:\u002F\u002Fgithub.com\u002FBarbapapazes\u002Fgarabit\u002Fblob\u002Fmain\u002F.vitepress\u002Fog-template.svg) and upload it to **Figma**.\n::\n\nExport two SVG files: one with outlined text and another without (ensure the option is unchecked). Both exports should be SVG files.\n\n### Merge the two SVG templates\n\nUpon exporting, we have two SVG files which need to be merged to develop our template.\n\n::callout{icon=\"i-ph-warning-duotone\" color=\"warning\"}\nThis is the most challenging part. Continue reading before starting to merge the **SVG files**.\n::\n\nThe smaller SVG contains readable text, which exists within `\u003Ctext>` tags. Retain these `\u003Ctext>` tags representing our placeholders:\n\n```xml [Example]\n\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"198.318\">{{line1}}\u003C\u002Ftspan>\u003C\u002Ftext>\n\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"283.318\">{{line2}}\u003C\u002Ftspan>\u003C\u002Ftext>\n\u003Ctext fill=\"black\" xml:space=\"preserve\" style=\"white-space: pre\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\">\u003Ctspan x=\"140\" y=\"368.318\">{{line3}}\u003C\u002Ftspan>\u003C\u002Ftext>\n```\n\nOpen both SVG files side by side to simplify the merging process. Find the equivalent `\u003Cpathx>` tags in the larger file, which represent the outlined or vectorized text.\n\nFor example, the `{{line1}}` text transforms into the path:\n\n```xml [Example]\n\u003Cpathx d=\"M141.982 180.358V176.629C144.396 176.629 146.08 176.139 172.602V179.761H357.958V172.602H364.712Z\" fill=\"black\"\u002F>\n```\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThe path example is shortened for clarity. Actual paths are considerably longer.\n::\n\nKeep the files open concurrently as text order corresponds in both. Replace `\u003Cpath>` tags in the second file with the `\u003Ctext>` tags from the first file. Retain any `\u003Cpath>` tags not associated with text placeholders.\n\n::callout{icon=\"i-ph-lightbulb-duotone\" color=\"success\"}\nAssign a color to the placeholders in **Figma** to easily identify them during merging. You can update the color in the SVG file via the `fill` property.\n::\n\nAfter combining, optimize the resultant SVG file using [**SVGO**](https:\u002F\u002Fjakearchibald.github.io\u002Fsvgomg\u002F).\n\nThis refined SVG template is now ready for use in our **VitePress** project. Save it to the `.vitepress` directory as `og-template.svg`.\n\n::callout{icon=\"i-ph-lightbulb-duotone\" color=\"success\"}\nAccess the [final SVG template in the project repository](https:\u002F\u002Fgithub.com\u002FBarbapapazes\u002Fgarabit\u002Fblob\u002Fmain\u002F.vitepress\u002Fog-template.svg).\n::\n\n```xml [Example]\n\u003C!-- Partial SVG, DO NOT USE AS IS -->\n\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" fill=\"none\" viewBox=\"0 0 1200 630\">\n  \u003C!-- Template elements -->\n  \u003Cpath fill=\"#FA8EFD\" d=\"M0 0h1200v630H0z\"\u002F>\n  \u003Cpath fill=\"#000\" d=\"M96 96h1040v363H96zm310 415h407v69H406z\"\u002F>\n  \u003Cpath fill=\"#FEEA35\" d=\"M401 506h401v63H401z\"\u002F>\n  \u003Cpath stroke=\"#000\" stroke-width=\"6\" d=\"M401 506h401v63H401z\"\u002F>\n  \u003Cpath fill=\"#000\" d=\"M451.415 534.119a4.504 4.504 0 0 0-.588-1.235 3.79 3.79 0 0 0-.887-.946 3.571 3.571 0 0 0-1.176-.589 4.917 4.917 0 0 0-1.449-.204c-.937 0-1.772.236-2.505.707-.733.472-1.31 1.165-1.73 2.08-.415.909-.623 2.017-.623 1.679c.375.733.56 1.622.554 2.668V546h-3.085v-7.858c0-.875-.227-1.56-.682-2.054-.449-.494-1.071-.741-1.866-.741-.54 0-1.02.119-1.441.358a2.477 2.477 0 0 0-.98 1.014c-.233.443-.349.98-.349 1.611Z\"\u002F>\n  \u003Cpath fill=\"#fff\" d=\"M83 83h1034v357H83z\"\u002F>\n  \u003Cpath stroke=\"#000\" stroke-width=\"6\" d=\"M83 83h1034v357H83z\"\u002F>\n  \u003C!-- Text placeholders -->\n  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"198.318\">{{line1}}\u003C\u002Ftspan>\u003C\u002Ftext>\n  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"283.318\">{{line2}}\u003C\u002Ftspan>\u003C\u002Ftext>\n  \u003Ctext xml:space=\"preserve\" fill=\"#000\" font-family=\"Inter\" font-size=\"60\" font-weight=\"600\" letter-spacing=\"0em\" style=\"white-space:pre\">\u003Ctspan x=\"140\" y=\"368.318\">{{line3}}\u003C\u002Ftspan>\u003C\u002Ftext>\n\u003C\u002Fsvg>\n```\n\n### Generate OG image dynamically\n\nTo maintain organization, add a file named `genOg.ts` in the `.vitepress` directory. This script contains functions necessary to split the title into three lines and generate the **OG image**.\n\nFirst, install the necessary dependencies:\n\n```bash [genOg.ts]\npnpm add -D sharp\n```\n\nSubsequently, create the function for the OG image generation.\n\nSteps to accomplish this include:\n\n1. Load the **SVG template**.\n\n```ts [Example]\nimport { readFileSync } from 'node:fs'\nimport { join } from 'node:path'\n\nconst ogSvg = readFileSync(join('.vitepress', 'og-template.svg'), 'utf-8')\n```\n\n2. Check for pre-existing **OG image**; generate if absent:\n\n```ts [Example]\nimport { existsSync } from 'node:fs'\n\nif (existsSync(output)) {\n  \u002F\u002F eslint-disable-next-line no-useless-return\n  return\n}\n```\n\n3. Create the directory for OG images if it does not exist:\n\n```ts [Example]\nimport { mkdirSync } from 'node:fs'\nimport { dirname } from 'node:path'\n\nmkdirSync(dirname(output), { recursive: true })\n```\n\n4. Break the title into three lines:\n\n```ts [Example]\nconst lines = title\n  .trim()\n  .split(\u002F(.{0,30})(?:\\s|$)\u002Fg)\n  .filter(Boolean)\n\nconst data: Record\u003Cstring, string> = {\n  line1: lines[0],\n  line2: lines[1],\n  line3: lines[2],\n}\n```\n\n5. Substitute placeholders in the SVG template with title lines:\n\n```ts [Example]\nconst svg = ogSvg.replace(\u002F\\{\\{([^}]+)\\}\\}\u002Fg, (_, name) => data[name] || '')\n```\n\n6. Transform the **SVG template** into a **PNG image** and store it:\n\n```ts [Example]\nimport { Buffer } from 'node:buffer'\nimport sharp from 'sharp'\n\nawait sharp(Buffer.from(svg)).resize(1440, 810).png().toFile(output)\n```\n\nHere is the comprehensive function:\n\n```ts [Example]\nimport { Buffer } from 'node:buffer'\nimport { existsSync, mkdirSync, readFileSync } from 'node:fs'\nimport { dirname, join } from 'node:path'\nimport sharp from 'sharp'\n\nconst ogSvg: string = readFileSync(\n  join('.vitepress', 'og-template.svg'),\n  'utf-8',\n)\n\n\u002F**\n * @credit Anthony Fu, https:\u002F\u002Fantfu.me\n * @link https:\u002F\u002Fgithub.com\u002Fantfu\u002Fantfu.me\u002Fblob\u002Fmain\u002Fvite.config.ts#L242\n *\u002F\nexport async function genOg(title: string, output: string) {\n  \u002F\u002F Skip if the file already exists\n  if (existsSync(output))\n    return\n\n  \u002F\u002F Ensure the output directory exists\n  mkdirSync(dirname(output), { recursive: true })\n\n  \u002F\u002F Break the title into lines of 30 characters\n  const lines = title\n    .trim()\n    .split(\u002F(.{0,30})(?:\\s|$)\u002Fg)\n    .filter(Boolean)\n\n  const data: Record\u003Cstring, string> = {\n    line1: lines[0],\n    line2: lines[1],\n    line3: lines[2],\n  }\n  const svg = ogSvg.replace(\u002F\\{\\{([^}]+)\\}\\}\u002Fg, (_, name) => data[name] || '')\n\n  console.info(`Generating ${output}`)\n  try {\n    await sharp(Buffer.from(svg)).resize(1440, 810).png().toFile(output)\n  }\n  catch (e) {\n    console.error('Failed to generate og image', e)\n  }\n}\n```\n\n### Assign OG images to each post\n\nNow that we have a functional method to generate OG images utilizing a title, it is crucial to determine when and where this function should be invoked.\n\nThe easiest strategy is to operate this function every time a file is processed by **VitePress** within the `transformPageData`, subsequent to the adjustments from the previous article.\n\nInitially, extract the name for the OG image using the slugged file path:\n\n```ts [Example]\nimport { defineConfig } from 'vitepress'\n\nexport default defineConfig({\n  async transformPageData(pageData) {\n    \u002F\u002F ...\n\n    const ogName = pageData.filePath\n      .replaceAll(\u002F\\\u002F\u002Fg, '-')\n      .replace(\u002F\\.md$\u002F, '.png')\n  }\n})\n```\n\nHere, a file path such as `blog\u002Flearn-vitepress.md` transforms into `blog-learn-vitepress.png`. This transformation is safe because Markdown filenames are already URL-compatible and adhere to filesystem norms.\n\nNext, invoke the function:\n\n```ts [blog\u002Flearn-vitepress.md]\nimport { joinURL } from 'ufo'\nimport { defineConfig } from 'vitepress'\nimport { genOg } from '.\u002FgenOg'\n\nexport default defineConfig({\n  async transformPageData(pageData, { siteConfig }) { \u002F\u002F Be careful, `{ siteConfig }` is a new parameter\n    \u002F\u002F ...\n\n    await genOg(\n      pageData.frontmatter.title || pageData.title || siteConfig.site.title,\n      joinURL(siteConfig.srcDir, 'public', 'og', ogName),\n    )\n  }\n})\n```\n\nTo ensure a fallback approach, we check for these potential titles: the frontmatter title, the inferred page title from the first h1, or, if neither are present, use the site title. This prevents the output of a blank **OG image**. Store generated OG images in the `public` directory under `og` to avoid name conflicts, as this folder merges into the `dist`.\n\nInclude `og` in the `.gitignore` file since it only contains generated outputs.\n\n### Incorporate metadata\n\nLastly, append **metadata** to inform social networks about the **OG image's** location, leveraging the previous **SEO** article advancements.\n\n```ts [Example]\nimport { joinURL } from 'ufo'\nimport { defineConfig } from 'vitepress'\n\nexport default defineConfig({\n  async transformPageData(pageData, { siteConfig }) {\n    \u002F\u002F ...\n\n    \u002F\u002F Integrate OG image URL into frontmatter\n    pageData.frontmatter.head.push(\n      [\n        'meta',\n        {\n          property: 'og:image',\n          content: joinURL(\n            'https:\u002F\u002Fgarabit.barbapapazes.dev', \u002F\u002F Please, change this before deploying\n            'og',\n            ogName,\n          ),\n        },\n      ],\n      [\n        'meta',\n        {\n          name: 'twitter:image',\n          content: joinURL(\n            'https:\u002F\u002Fgarabit.barbapapazes.dev', \u002F\u002F Please, change this before deploying\n            'og',\n            ogName,\n          ),\n        },\n      ],\n    )\n  }\n})\n```\n\n## An OG image for every path\n\nThis guide was anticipated eagerly as **OG images** are both pivotal and laborious to produce. The mechanisms here, which I have employed for several months, ensure the swift regeneration of **OG images** upon template changes. Over time, I have continuously tailored the template to align with evolving needs. Expanding on this methodology, I recently incorporated a headline for series titles following similar techniques.\n\nI can only recommend this simple approach, as it has significantly improved my workflow. I hope it will do the same for you.\n\n![OG Image from this Article](\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post\u002Farticle-og.png)\n",{"title":5,"description":2136},"create-a-blog-with-vitepress-and-vue-js-from-scratch",{"loc":2141,"images":2146},[2147,2148],{"loc":162},{"loc":2116},"series\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fcreating-open-graph-images-automatically-for-each-blog-post","DQqAhpyyObIlXRAldWbCA1JmjtD62dGryHGoyLNt_Ps",{"overview":2152,"chapters":2389},{"id":2153,"title":2154,"body":2155,"contentId":2369,"date":2370,"description":2371,"extension":1649,"github":2372,"meetTheInstructor":2375,"meta":2376,"navigation":512,"part":2139,"path":2377,"rawbody":2378,"robots":2139,"schemaOrg":2139,"seo":2379,"seriesSlug":2144,"sitemap":2380,"stem":2383,"subtitle":2384,"topics":2385,"__hash__":2388},"series\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch.md","Create a Blog with VitePress and Vue.js from Scratch",{"type":7,"value":2156,"toc":2366},[2157,2160,2174,2188,2324,2335,2343,2350,2353,2356,2360],[10,2158,2159],{},"Hello and welcome! 👋",[10,2161,2162,2163,2166,2167,2169,2170,2173],{},"You're about to embark on the \"Create a ",[14,2164,2165],{},"Blog"," with ",[14,2168,119],{}," and ",[14,2171,2172],{},"Vue.js"," from Scratch\" series!",[10,2175,2176,2177,2183,2184,2187],{},"Throughout this journey, we will construct a practical blog from the ground up using ",[81,2178,2181],{"href":2179,"rel":2180},"https:\u002F\u002Fvitepress.dev\u002F",[85],[14,2182,119],{},". We will explore a range of topics from initial configuration to deploying on ",[14,2185,2186],{},"Cloudflare Pages",", including:",[2189,2190,2191,2216,2226,2232,2245,2251,2261,2271,2280,2292,2309,2315],"ul",{},[97,2192,2193,2196,2197,2200,2201,2169,2208,2215],{},[14,2194,2195],{},"Getting Started with VitePress: Project Setup and Essentials",": Learn to set up a fresh ",[14,2198,2199],{},"VitePress project"," by incorporating ",[81,2202,2205],{"href":2203,"rel":2204},"https:\u002F\u002Ftailwindcss.com\u002F",[85],[14,2206,2207],{},"Tailwind CSS",[81,2209,2212],{"href":2210,"rel":2211},"https:\u002F\u002Fgithub.com\u002Funplugin\u002Funplugin-vue-components",[85],[14,2213,2214],{},"unplugin-vue-components"," for an enhanced development process.",[97,2217,2218,2221,2222],{},[14,2219,2220],{},"From Default to Custom: Building a VitePress Blog Theme",": We will entirely remove the default theme and create a personalized theme. ",[2223,2224,2225],"em",{},"You'll be surprised by its simplicity!",[97,2227,2228,2231],{},[14,2229,2230],{},"Optimize Your VitePress Blog's Architecture Design",": Discover how to organize code and design your blog for scalability and maintainability.",[97,2233,2234,2237,2238,25],{},[14,2235,2236],{},"Implementing Blog Index with VitePress and Tailwind CSS",": Create a list of blog posts. This will introduce ",[81,2239,2242],{"href":2240,"rel":2241},"https:\u002F\u002Fvitepress.dev\u002Fguide\u002Fdata-loading",[85],[14,2243,2244],{},"data loaders from VitePress",[97,2246,2247,2250],{},[14,2248,2249],{},"Single Blog Rendering in VitePress with Tailwind Typography",": Create a page dedicated to reading each blog post.",[97,2252,2253,2256,2257,2260],{},[14,2254,2255],{},"Deploy VitePress Blog Globally with Cloudflare Pages",": Integrate ",[14,2258,2259],{},"SEO metadata"," for improved search engine visibility, eliminating the need for manual updates.",[97,2262,2263,2266,2267,2270],{},[14,2264,2265],{},"Showcase Your GitHub Projects on Your VitePress Blog",": Develop a page to display your ",[14,2268,2269],{},"GitHub"," endeavors, extending the use of data loaders and demonstrating their flexibility.",[97,2272,2273,2275,2276,2279],{},[14,2274,5],{},": Efficiently create ",[14,2277,2278],{},"Open Graph images"," for each post, saving time and enhancing appeal.",[97,2281,2282,2284,2285,2291],{},[14,2283,2255],{},": Seamlessly deploy your blog to ",[81,2286,2289],{"href":2287,"rel":2288},"https:\u002F\u002Fpages.cloudflare.com\u002F",[85],[14,2290,2186],{},", achieving this in just a few clicks!",[97,2293,2294,2297,2298,2166,2301,2308],{},[14,2295,2296],{},"Ensure Blog Quality with Playwright Automated Testing",": Implement ",[14,2299,2300],{},"end-to-end testing",[81,2302,2305],{"href":2303,"rel":2304},"https:\u002F\u002Fplaywright.dev\u002F",[85],[14,2306,2307],{},"Playwright"," to ensure your blog functions as expected.",[97,2310,2311,2314],{},[14,2312,2313],{},"An Internal Refactor for the Future Growth of our Blog",": Prepare your blog for future growth by refactoring the codebase, ensuring it remains maintainable and scalable.",[97,2316,2317,2320,2321,2323],{},[14,2318,2319],{},"Migrating Our VitePress Blog to Tailwind CSS Version 4",": Upgrade our blog to the latest version of ",[14,2322,2207],{}," to leverage its new features and improvements.",[10,2325,2326,2327,2330,2331,2334],{},"By the conclusion, you will possess a fully operational and captivating ",[14,2328,2329],{},"blog",", allowing you to concentrate exclusively on producing ",[14,2332,2333],{},"content",", which is paramount. Simply write and publish, as the blog will autonomously manage the rest.",[156,2336,2337,2341],{},[159,2338],{"alt":2339,"src":2340},"Home Page of our Blog","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fdeploy-vitepress-blog-globally-with-cloudflare-pages\u002Fvitepress-home.png",[164,2342,2339],{},[10,2344,2345,2346,2349],{},"This series offers a detailed, step-by-step ",[14,2347,2348],{},"guide",". Rest assured, each aspect will be thoroughly elucidated, from selecting to implementing every feature. While each feature will be minimal and focused on the core, this does not mean we will forgo creating a visually pleasing and functional blog.",[10,2351,2352],{},"Should you have any inquiries, please feel free to engage in the comments section under each article.",[10,2354,2355],{},"I hope you find this series truly enjoyable! 🚀",[50,2357,2359],{"id":2358},"chapters","Chapters",[2361,2362],"series-chapters-list",{"className":2363},[2364,2365],"mx-auto","max-w-5xl",{"title":217,"searchDepth":230,"depth":230,"links":2367},[2368],{"id":2358,"depth":230,"text":2359},"43c95b33-041c-4108-ab98-0d21ef60ed7f","2024-10-10","Build a dynamic blog with VitePress and Vue.js from scratch. Learn setup, SEO, and deployment for a seamless blogging experience.",{"name":2373,"repository":2374},"garabit","barbapapazes\u002Fgarabit","I create this series because \u003Cstrong>VitePress\u003C\u002Fstrong> is way more powerful than it seems at the first glance. I was amazed by the \u003Cstrong>flexibility\u003C\u002Fstrong> and the \u003Cstrong>simplicity\u003C\u002Fstrong> of the tool while building my blog and wanted the community to know about it.",{},"\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch","---\ncontentId: 43c95b33-041c-4108-ab98-0d21ef60ed7f\ntitle: Create a Blog with VitePress and Vue.js from Scratch\nsubtitle: A new perspective on blogging with VitePress and Vue.js\ndescription: Build a dynamic blog with VitePress and Vue.js from scratch. Learn setup, SEO, and deployment for a seamless blogging experience.\ndate: 2024-10-10\ntopics:\n  - vuejs\n  - vitepress\n  - blog\ngithub:\n  name: garabit\n  repository: barbapapazes\u002Fgarabit\nmeetTheInstructor: I create this series because \u003Cstrong>VitePress\u003C\u002Fstrong> is way more powerful than it seems at the first glance. I was amazed by the \u003Cstrong>flexibility\u003C\u002Fstrong> and the \u003Cstrong>simplicity\u003C\u002Fstrong> of the tool while building my blog and wanted the community to know about it.\nseriesSlug: create-a-blog-with-vitepress-and-vue-js-from-scratch\n---\n\nHello and welcome! 👋\n\nYou're about to embark on the \"Create a **Blog** with **VitePress** and **Vue.js** from Scratch\" series!\n\nThroughout this journey, we will construct a practical blog from the ground up using [**VitePress**](https:\u002F\u002Fvitepress.dev\u002F). We will explore a range of topics from initial configuration to deploying on **Cloudflare Pages**, including:\n\n- **Getting Started with VitePress: Project Setup and Essentials**: Learn to set up a fresh **VitePress project** by incorporating [**Tailwind CSS**](https:\u002F\u002Ftailwindcss.com\u002F) and [**unplugin-vue-components**](https:\u002F\u002Fgithub.com\u002Funplugin\u002Funplugin-vue-components) for an enhanced development process.\n- **From Default to Custom: Building a VitePress Blog Theme**: We will entirely remove the default theme and create a personalized theme. _You'll be surprised by its simplicity!_\n- **Optimize Your VitePress Blog's Architecture Design**: Discover how to organize code and design your blog for scalability and maintainability.\n- **Implementing Blog Index with VitePress and Tailwind CSS**: Create a list of blog posts. This will introduce [**data loaders from VitePress**](https:\u002F\u002Fvitepress.dev\u002Fguide\u002Fdata-loading).\n- **Single Blog Rendering in VitePress with Tailwind Typography**: Create a page dedicated to reading each blog post.\n- **Deploy VitePress Blog Globally with Cloudflare Pages**: Integrate **SEO metadata** for improved search engine visibility, eliminating the need for manual updates.\n- **Showcase Your GitHub Projects on Your VitePress Blog**: Develop a page to display your **GitHub** endeavors, extending the use of data loaders and demonstrating their flexibility.\n- **Creating Open Graph Images Automatically for Each Blog Post**: Efficiently create **Open Graph images** for each post, saving time and enhancing appeal.\n- **Deploy VitePress Blog Globally with Cloudflare Pages**: Seamlessly deploy your blog to [**Cloudflare Pages**](https:\u002F\u002Fpages.cloudflare.com\u002F), achieving this in just a few clicks!\n- **Ensure Blog Quality with Playwright Automated Testing**: Implement **end-to-end testing** with [**Playwright**](https:\u002F\u002Fplaywright.dev\u002F) to ensure your blog functions as expected.\n- **An Internal Refactor for the Future Growth of our Blog**: Prepare your blog for future growth by refactoring the codebase, ensuring it remains maintainable and scalable.\n- **Migrating Our VitePress Blog to Tailwind CSS Version 4**: Upgrade our blog to the latest version of **Tailwind CSS** to leverage its new features and improvements.\n\nBy the conclusion, you will possess a fully operational and captivating **blog**, allowing you to concentrate exclusively on producing **content**, which is paramount. Simply write and publish, as the blog will autonomously manage the rest.\n\n![Home Page of our Blog](\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fdeploy-vitepress-blog-globally-with-cloudflare-pages\u002Fvitepress-home.png)\n\nThis series offers a detailed, step-by-step **guide**. Rest assured, each aspect will be thoroughly elucidated, from selecting to implementing every feature. While each feature will be minimal and focused on the core, this does not mean we will forgo creating a visually pleasing and functional blog.\n\nShould you have any inquiries, please feel free to engage in the comments section under each article.\n\nI hope you find this series truly enjoyable! 🚀\n\n## Chapters\n\n::series-chapters-list\n---\nclass: mx-auto max-w-5xl\n---\n::\n",{"title":2154,"description":2371},{"loc":2377,"images":2381},[2382],{"loc":2340},"series\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch","A new perspective on blogging with VitePress and Vue.js",[2386,2387,2329],"vuejs","vitepress","H9cubHgLKM0Siblzkwf7mDsFVYr2CLbh_ciFOpQrSVk",[2390,2396,2402,2408,2414,2420,2427,2433,2435,2441,2447,2454],{"contentId":2391,"title":2195,"description":2392,"path":2393,"part":224,"github":2394,"topics":2139},"b1b99d95-ac6d-4f25-a121-0851988102a2","From setup to styling with Tailwind CSS and automatic Vue imports, build your content-driven site effortlessly.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fgetting-started-with-vitepress-project-setup-and-essentials",{"branch":2395},"01.project-initialization",{"contentId":2397,"title":2220,"description":2398,"path":2399,"part":230,"github":2400,"topics":2139},"172f2a41-2f7c-44e9-855d-322adc536279","Step-by-step VitePress theme customization: guide to building a unique theme layout, adding Markdown content, and removing defaults.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Ffrom-default-to-custom-building-a-vitepress-blog-theme",{"branch":2401},"02.the-basics-for-a-custom-theme",{"contentId":2403,"title":2230,"description":2404,"path":2405,"part":236,"github":2406,"topics":2139},"900d11a5-ff68-4cc9-8ace-66a5cb1577fa","Discover how to optimize your VitePress blog's architecture design with scalable solutions and efficient Vite plugin integration.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Foptimize-your-vitepress-blog-architecture-design",{"branch":2407},"03.define-the-design-of-the-blog-architecture",{"contentId":2409,"title":2236,"description":2410,"path":2411,"part":346,"github":2412,"topics":2139},"4d93a3c5-f3ff-4ff6-a569-5234ef805850","Learn to create a list of blog articles using VitePress, a crucial feature for any content-centric website. Utilize data loaders to load articles.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fimplementing-blog-index-with-vitepress-and-tailwind-css",{"branch":2413},"04.comprehensive-guide-to-listing-blog-articles",{"contentId":2415,"title":2249,"description":2416,"path":2417,"part":352,"github":2418,"topics":2139},"15b9a49c-b9fb-4d76-a08d-2acaf55ff205","Enhance your VitePress blog with Tailwind Typography. Learn to render markdown beautifully with syntax highlights.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fsingle-blog-rendering-in-vitepress-with-tailwind-typography",{"branch":2419},"05.read-a-single-blog-article",{"contentId":2421,"title":2422,"description":2423,"path":2424,"part":358,"github":2425,"topics":2139},"66af9aaf-d6d3-4151-a51b-806835988340","Enhance Website Visibility: SEO Metadata and Sitemap","Enhance website traffic through strategic SEO metadata and sitemaps for improved indexing. Learn how to automate metadata generation for each page.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fenhance-website-visibility-seo-metadata-and-sitemap",{"branch":2426},"06.seo-metadata-and-sitemap",{"contentId":2428,"title":2265,"description":2429,"path":2430,"part":364,"github":2431,"topics":2139},"a4a933b8-07a5-4587-ad54-c3f9e30cbf45","Enhance your VitePress blog with GitHub projects. Learn API data retrieval and how to display your projects on your blog.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fshowcase-your-github-projects-on-your-vitepress-blog",{"branch":2432},"07.showcase-your-github-projects",{"contentId":2134,"title":5,"description":2136,"path":2141,"part":370,"github":2434,"topics":2139},{"branch":2138},{"contentId":2436,"title":2255,"description":2437,"path":2438,"part":376,"github":2439,"topics":2139},"c05ae463-c5ae-4c43-872d-bb05263c7f74","Deploy the blog to Cloudflare Pages for global accessibility. Discover how to deploy the blog to Cloudflare Pages and share it worldwide with ease.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fdeploy-vitepress-blog-globally-with-cloudflare-pages",{"branch":2440},"09.share-the-blog-globally",{"contentId":2442,"title":2296,"description":2443,"path":2444,"part":382,"github":2445,"topics":2139},"aa5392e0-6287-4673-9365-34313f0af1fa","Enhance web development with Playwright-induced automated testing. Elevate blog quality through comprehensive end-to-end and SEO tests.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fensure-blog-quality-with-playwright-automated-testing",{"branch":2446},"10.automate-testing",{"contentId":2448,"title":2449,"description":2450,"path":2451,"part":388,"github":2452,"topics":2139},"5b1b6117-14f0-4f1d-ba13-5b3583f341bf","An Internal Refactor for the Future Growth of Our Blog","Prepare your blog for future growth by refactoring the codebase to ensure it remains maintainable and scalable.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fan-internal-refactor-for-the-future-growth-of-our-blog",{"branch":2453},"11.internal-refactor",{"contentId":2455,"title":2319,"description":2456,"path":2457,"part":394,"github":2458,"topics":2139},"3499bbd1-20a6-43f0-9bb7-be1da2bb6eac","A step-by-step guide on upgrading our VitePress blog to Tailwind CSS 4.0 to leverage its latest features and improvements.","\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch\u002Fmigrating-our-vitepress-blog-to-tailwind-css-version-4",{"branch":2459},"12.migration-to-tailwind-css-4",1795016692574]