主要内容

splitMarkdownSections

Split Markdown document into sections

Since R2026a

    Description

    chunkTable = splitMarkdownSections(str) splits an input Markdown document str into sections, for example according to the ATX section tags "# ", "## ", …, "###### ".

    example

    chunkTable = splitMarkdownSections(t) splits a table of Markdown documents t into sections.

    example

    Examples

    collapse all

    To split a Markdown-formatted document into sections, use splitMarkdownSections and specify the Markdown code as a string.

    str = "# Title" + newline + ...
        "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline +  ...
        "### Section 1" + newline + "Content of section 1." + newline +  ...
        "### Section 2" + newline + "Content of section 2.";
    chunkTable = splitMarkdownSections(str)
    chunkTable=3×4 table
                         Text                       H1           H2             H3     
        ______________________________________    _______    ___________    ___________
    
        "Introductory paragraph of chapter 1."    "Title"    "Chapter 1"    <missing>  
        "Content of section 1."                   "Title"    "Chapter 1"    "Section 1"
        "Content of section 2."                   "Title"    "Chapter 1"    "Section 2"
    
    

    To split multiple Markdown documents into chunks in a single chunk table, then first create a table of documents and then use splitMarkdownSections with the table of documents as the input. To retain metadata about the source documents, such as their filenames, add the metadata to the table as additional variables.

    Create a table from:

    • A variable Text that contains Markdown documents.

    • A variable DocumentName that contains the names of the documents.

    str1 = "# Title" + newline + ...
        "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline +  ...
        "### Section 1" + newline + "This is the first chapter of the first document.";
    str2 = "# Title" + newline + ...
        "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline +  ...
        "### Section 1" + newline + "This is the first chapter of the second document.";
    str3 = "# Title" + newline + ...
        "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline +  ...
        "### Section 1" + newline + "This is the first chapter of the third document.";
    Text = [str1;str2;str3];
    DocumentName = ["Document 1";"Document 2";"Document 3"];
    t = table(Text,DocumentName)
    t=3×2 table
                                                                   Text                                                                DocumentName
        ___________________________________________________________________________________________________________________________    ____________
    
        "# Title↵## Chapter 1↵Introductory paragraph of chapter 1.↵### Section 1↵This is the first chapter of the first document."     "Document 1"
        "# Title↵## Chapter 1↵Introductory paragraph of chapter 1.↵### Section 1↵This is the first chapter of the second document."    "Document 2"
        "# Title↵## Chapter 1↵Introductory paragraph of chapter 1.↵### Section 1↵This is the first chapter of the third document."     "Document 3"
    
    

    Split the table of Markdown documents into sections using the splitMarkdownSections function.

    chunkTable = splitMarkdownSections(t)
    chunkTable=6×5 table
                               Text                            DocumentName      H1           H2             H3     
        ___________________________________________________    ____________    _______    ___________    ___________
    
        "Introductory paragraph of chapter 1."                 "Document 1"    "Title"    "Chapter 1"    <missing>  
        "This is the first chapter of the first document."     "Document 1"    "Title"    "Chapter 1"    "Section 1"
        "Introductory paragraph of chapter 1."                 "Document 2"    "Title"    "Chapter 1"    <missing>  
        "This is the first chapter of the second document."    "Document 2"    "Title"    "Chapter 1"    "Section 1"
        "Introductory paragraph of chapter 1."                 "Document 3"    "Title"    "Chapter 1"    <missing>  
        "This is the first chapter of the third document."     "Document 3"    "Title"    "Chapter 1"    "Section 1"
    
    

    Input Arguments

    collapse all

    Input document, specified as a string array, character vector, or cell array of character vectors.

    Data Types: string | char | cell

    Input table of documents. t must have a column named Text that contains the documents. The documents must be specified as a string scalar.

    Output Arguments

    collapse all

    Table of text chunks. chunkTable has these variables:

    • Text — Text chunk, returned as a string scalar.

    • H1, H2, ..., H6 — Title of nth-level Markdown section that contains the chunk, delineated in the Markdown code by #, ##, ..., ######. If the chunk is not part of an nth-level Markdown section, then the corresponding variable Hn contains <missing>. If there are no nth-level Markdown sections in the input document, then no variable Hn exists.

    If you specify the input documents as a table, then chunkTable also contains all the variables in the table. For each chunk, the values of the variables are the same as for the document from which the chunk originates.

    More About

    collapse all

    Version History

    Introduced in R2026a