`. If this is part of the series, the frontmatter must contain two more fields: `part: ` and `final: `. - The outline must contain a list of section headings. - Each section heading must be preceded by a blank line. - Each top-level section heading must start with `###`, subsection headings must start with `####`. - Lines with `@section-code` means references to `*-section-code.md` file. Do NOT create it if no file `*-section-code.md` exists in the same folder. - Content lines without `#` and `@` are optional. If they exist, they are elig"> `. If this is part of the series, the frontmatter must contain two more fields: `part: ` and `final: `. - The outline must contain a list of section headings. - Each section heading must be preceded by a blank line. - Each top-level section heading must start with `###`, subsection headings must start with `####`. - Lines with `@section-code` means references to `*-section-code.md` file. Do NOT create it if no file `*-section-code.md` exists in the same folder. - Content lines without `#` and `@` are optional. If they exist, they are elig"> `. If this is part of the series, the frontmatter must contain two more fields: `part: ` and `final: `. - The outline must contain a list of section headings. - Each section heading must be preceded by a blank line. - Each top-level section heading must start with `###`, subsection headings must start with `####`. - Lines with `@section-code` means references to `*-section-code.md` file. Do NOT create it if no file `*-section-code.md` exists in the same folder. - Content lines without `#` and `@` are optional. If they exist, they are elig">
---
description: Use if a piece folder or a .mkd file is in the prompt. 
globs: ["*.*-*", "*_outline.mkd"]
alwaysApply: false
---

# Rules
- Folder name `N.n-piece-code`, outline file name `n_outline.mkd` and section file names `n-section-code.md` have the same number n. 

## Outline Structure
- The outline must contain YAML frontmatter enclosed by `---` lines. It has at least two fields: `layout: article` and `title: <some title>`. If this is part of the series, the frontmatter must contain two more fields: `part: <N>` and `final: <true or false>`.
- The outline must contain a list of section headings.
  - Each section heading must be preceded by a blank line.
  - Each top-level section heading must start with `###`, subsection headings must start with `####`.
- Lines with `@section-code` means references to `*-section-code.md` file. Do NOT create it if no file `*-section-code.md` exists in the same folder.
- Content lines without `#` and `@` are optional. If they exist, they are eligible to %Content Rules%.

## Actions
1. If asked to (re)create sections:
  - Check if `n_outline.mkd` file exists in the folder. If yes, analyze it, otherwise output an error message and stop.
  - If no `@section-code` reference are found in `n_outline.mkd`, output a warning message and stop.
  - For each top-level section of `n_outline.mkd` that has a `@section-code` reference, create a section file `n-section-code.md` and fill it as follows:
    - If the folder contains `n_draft.mkd` file, find its part corresponding to the section and use everything in this part to fill the section file. 
      - If the draft contains <only Medium> or <only Substack> tags, then use the content inside those tages only if the section-code ends with the suffix `-medium` or `-substack`, respectively. 
      - You may need to translate the draft content into English, or just copy it with grammar and style corrections if it's already in English. 
      - If the draft has a list of brief ideas rather than a complete text, you may need to expand those ideas.
      - DO NOT create any images references in the draft.
    - Otherwise, fill the section file with all the outline's content that corresponds to this `@section-code` reference, except for the reference itself — including section heading(s).
  - If the folder already contains `n-section-code.md` file, and it is referenced as `@section-code` in the outline, update it rather than creating a new file. "Update" means:
    - Updating the main section heading.
    - Creating new subsections if they are missing in the section file. The content of new subsection must be generated from the content of this subsection in the outline (if any).
    - Existing subsections must be preserved

2. If asked to write an outline from content (i.e. piece folder or section files):
  - Check if at least one section file exists in the folder. If no, output an error message.
  - Check if `n_outline.mkd` file exists in the folder. If yes, analyze it, otherwise create such a file and write the frontmatter in it.
  - Fill the outline based on section files of the folder.
  - When processing `n-section-code.md` file, find a right place to create a reference `@section-code`. It should be under the (first) top-level section heading from this file. Do NOT create @-references for non-existing files.
  - If the outline already contains a section with `@section-code` reference, update it rather than creating a new section. "Update" means:
    - Updating the section heading.
    - If there are subsections in the outline, replacing all subsections with new ones.
  - Each top-level section heading must be followed by a reference of the corresponding section file (`@section-code`) and a summary of the section ONLY if the section file DOES NOT contains subsection headings.
  - Each subsection heading must be followed a summary of the subsection from the section file. 
  - A summary must be written according to %Content Rules%
  - If a section file does not contain any content, the heading must be equal to section-code from the file name, and it must be followed by `<!-- empty -->` comment.

3. If asked to generate an outline from the braindump `n_dump.mkd` or `n_draft.mkd` file added to the prompt:
  - Check if `n_outline.mkd` file exists in the folder. If yes, analyze it, otherwise create such a file and write the frontmatter in it.
  - If the outline already contains a frontmatter or any content, don't change it, just add generated content to the end.
  - What you must generate from the braindump:
    - First, try finding a title in the beginning of the braindump. If no title there, generate one and write it as `#` heading.
    - Generate a list of sections. Sections could be of 2 types:
      1. Sections found in the braindump (as `###` or `####` headings). Do NOT remove indices from the headings if they are present there.
      2. Sections generated from the braindump content. Insert a section heading yourself if there is a headingless portion of the braindump that consists of more than 400 words or discuss multiple weakly connected topics.
    - Under each top-level section, generate a reference (`@section-code`); this means a section file to create later. section-code must start with an index of the section, for example: `@0-intro` or `3-use-cases`.
    - If a section is placed into <only Medium> or <only Substack> tag in the braindump, you must add a suffix `-medium` or `-substack`, respectively, to the section-code. For example: `@6-conclusion-medium` and `@6-conclusion-substack`. Put the heading of the section above each section-code.

## Content Rules
- Outline content consists of source section summaries, each of ~2 sentences length, in English.
- When writing an outline, if a source section is no more than 4 sentences, just copy its content instead of summarizing.
- If the source includes `<TBD>` placeholder, it is a signal for you to generate text.
- When creating sections, `<TODO>`, `<image>`, `![](...)` and `<subscribe>` placeholders must be kept AS IS.