interface Heading { level: number; text: string;}
interface HeadingNode extends Heading { children: HeadingNode[];}
export class TOC { private static readonly HEADING_REGEX: RegExp = /^(#{1,6})\s+(.+)$/;
private constructor() {}
private static parseHeadings(contentLines: string[]): Heading[] { const parsedHeadings: Heading[] = [];
for (const heading of contentLines) { const match: RegExpMatchArray | null = heading.match(this.HEADING_REGEX);
if (!match) continue;
const level: number = match[1]!.length; const text: string = match[2]!.trim(); parsedHeadings.push({ level: level, text: text }); }
return parsedHeadings; }
public static generate(markdownContent: string): HeadingNode[] { const contentLines: string[] = markdownContent.split("\n"); const parsedHeadings: Heading[] = this.parseHeadings(contentLines);
const generatedNodes: HeadingNode[] = []; const stack: HeadingNode[] = [];
for (const heading of parsedHeadings) { const node: HeadingNode = { text: heading.text, level: heading.level, children: [], };
while (stack.length > 0 && stack[stack.length - 1]!.level >= heading.level) { stack.pop(); }
if (stack.length === 0) { generatedNodes.push(node); } else { stack[stack.length - 1]!.children.push(node); }
stack.push(node); }
return generatedNodes; }}Bu yardımcı araç, Markdown içeriklerindeki başlıkları algılayarak hiyerarşik bir içindekiler ağacı oluşturur. İçerikteki tüm başlıkları otomatik olarak algılar ve üst-alt başlık ilişkilerini koruyan yapılandırılmış bir çıktı sunar. Bu çıktıyı arayüzde dinamik bir içindekiler bölümü oluşturmak için kullanabilirsiniz.
Örnek olarak aşağıdaki pizza tarifi yazısını kullanabiliriz:
# The Perfect Homemade Pizza
Pizza is one of the most popular dishes around the world. Although ordering from a restaurant is convenient, making pizza at home can be a fun and rewarding experience. With fresh ingredients and a little patience, you can prepare a delicious pizza with a crispy crust and perfectly melted cheese.
## Introduction
Homemade pizza gives you complete control over the ingredients, flavors, and texture. You can experiment with different toppings, adjust the thickness of the dough, and create something that suits your taste.
The secret to a great pizza is balancing three essential elements: a well-prepared dough, a flavorful tomato sauce, and fresh toppings.
## Ingredients
For the dough, you will need flour, warm water, dry yeast, olive oil, salt, and a little sugar. These simple ingredients come together to create a soft and elastic base.
For the sauce, prepare ripe tomatoes or canned crushed tomatoes, garlic, olive oil, salt, black pepper, and dried oregano.
For the toppings, mozzarella cheese is a classic choice. You can also use mushrooms, bell peppers, olives, onions, pepperoni, or fresh basil.
## Preparation
Before starting, measure all your ingredients and prepare a clean working surface. Having everything ready will make the cooking process much easier.
### Preparing the Dough
1. Combine warm water, yeast, and sugar in a bowl. Let the mixture rest for approximately five minutes until it becomes slightly foamy.
2. Add the flour, salt, and olive oil. Mix everything until a rough dough forms, then knead it for about ten minutes until smooth and elastic.
3. Place the dough in a lightly oiled bowl, cover it with a clean kitchen towel, and let it rise for one to two hours. It should roughly double in size.
### Making the Sauce
1. Heat olive oil in a small saucepan over medium heat. Add finely chopped garlic and cook briefly until fragrant.
2. Add the tomatoes, oregano, salt, and black pepper. Let the sauce simmer for approximately 15–20 minutes, stirring occasionally.
3. Once the sauce thickens, remove it from the heat and allow it to cool before spreading it over the dough.
### Preparing the Toppings
Wash and slice your vegetables into thin, evenly sized pieces. If you are using mushrooms, make sure they are clean and dry.
Grate or slice the mozzarella cheese. Avoid adding too many toppings, as excessive moisture can prevent the pizza from cooking properly.
Keep all your prepared ingredients nearby so you can assemble the pizza quickly.
## Cooking Instructions
Preheat your oven to its highest suitable temperature, ideally around 240–250°C. If you have a pizza stone or steel, place it inside while the oven heats up.
Stretch the dough into your desired shape and place it on a baking sheet or pizza peel. Spread a thin layer of tomato sauce over the surface, leaving a small border around the edges.
Add the mozzarella and your selected toppings. Transfer the pizza to the oven and bake for approximately 10–15 minutes, depending on your oven and dough thickness.
### Checking the Crust
A properly baked pizza should have a golden-brown crust with a slightly crispy exterior and a soft interior.
Lift one edge carefully to check the bottom. If it is still pale or feels doughy, give it a few more minutes in the oven.
## Serving Suggestions
Remove the pizza from the oven and let it rest for two or three minutes before slicing. This allows the cheese to settle and makes serving easier.
Fresh basil leaves, a drizzle of olive oil, or a little grated Parmesan can add extra flavor.
Serve your pizza with a fresh green salad, roasted vegetables, or garlic bread. A cold lemonade or your favorite soft drink also makes a great accompaniment.
## Tips and Tricks
Always use warm rather than hot water when activating yeast. Excessive heat can kill the yeast and prevent the dough from rising.
Avoid adding too much sauce or too many toppings. A lighter topping layer generally produces a better texture and allows the crust to cook evenly.
If you prefer a crispier base, preheat your baking tray or use a pizza stone. Make sure your oven is fully heated before putting the pizza inside.
Experiment with different cheeses, herbs, and vegetables to discover your favorite combination. There is no single correct way to make homemade pizza.
### Storage and Reheating
If you have leftover pizza, allow it to cool completely before storing it in an airtight container. Keep it refrigerated and consume it within three to four days.
To reheat pizza, place the slices in a skillet over medium-low heat. Cover the pan for a few minutes to help the cheese melt while keeping the crust crispy.
You can also use an oven preheated to 180°C. Avoid using the microwave if you want to preserve the original crust texture, as it tends to make the base soft.Bu içeriği sayfaya yüklediğimizi varsayalım. Ardından içindekiler tablosunu aşağıdaki şekilde oluşturabiliriz:
import { TOC } from "./toc-generator.ts";import { loader } from "./custom-loader.ts";
const icerik = loader.loadPizzaRecipe();
const icindekiler = TOC.generate(icerik);Oluşturduğumuz icindekiler değişkeni aşağıdaki veriyi içerecektir:
[ { "text": "The Perfect Homemade Pizza", "level": 1, "children": [ { "text": "Introduction", "level": 2, "children": [] }, { "text": "Ingredients", "level": 2, "children": [] }, { "text": "Preparation", "level": 2, "children": [ { "text": "Preparing the Dough", "level": 3, "children": [] }, { "text": "Making the Sauce", "level": 3, "children": [] }, { "text": "Preparing the Toppings", "level": 3, "children": [] } ] }, { "text": "Cooking Instructions", "level": 2, "children": [ { "text": "Checking the Crust", "level": 3, "children": [] } ] }, { "text": "Serving Suggestions", "level": 2, "children": [] }, { "text": "Tips and Tricks", "level": 2, "children": [ { "text": "Storage and Reheating", "level": 3, "children": [] } ] } ] }]