getTableOfContentsModelJSON
Release 3.2.0 and above, Release 2.20.0 and above.
Returns the current editor's table of contents (TOC) model as a JSON Array. (Tree-structured model)
Parameters:
<table> <thead> <tr><th>Name</th><th>Type</th><th>Attribute</th><th>Description</th></tr> </thead> <tbody> <tr> <td><code>shouldAssignId</code></td> <td>boolean</td> <td>default: <code>false</code></td> <td>Sets whether to assign new IDs to the TOC model.
- When
true, IDs with the'toc_'prefix are applied. To keep the TOC IDs in the content stable across save/load, you must callupdateTableOfContentsIdsto sync the IDs. - When
false, the IDs used internally by the editor are applied, and they are refreshed on each save/load. When the IDs are not the editor's internal IDs, the existing IDs are preserved across save/load.
Return:
<table> <thead> <tr><th>Type</th><th>Description</th></tr> </thead> <tbody> <tr> <td>Array</td> <td>TOC model JSON Array.
ex) Tree-structured model
[
{
id: TOC ID,
text: text,
children: [...],
level: TOC level
}
]
</td>
</tr>
</tbody>
</table>
To synchronize the IDs of TOC elements in the content with the TOC model, call updateTableOfContentsIds.
This method updates the IDs of the actual TOC elements in the editor to match the latest TOC model.
ex) editor.updateTableOfContentsIds();
When setting 'editor.titleStyle', elements with a specific class can be extracted as TOC entries.
For more details: Configuration.
Example:

// How to get the model
// 1. TOC model with the IDs used internally by the editor
var model = editor.getTableOfContentsModelJSON();
// 2. TOC model with IDs that have the TOC-only prefix
var model = editor.getTableOfContentsModelJSON(true);
// How to keep TOC IDs in the content stable across save/load
// 1. Get the TOC model with IDs that have the TOC-only prefix
editor.getTableOfContentsModelJSON(true);
// 2. Synchronize the IDs of TOC elements in the content with the TOC model
editor.updateTableOfContentsIds();
shouldAssignId: false
[
{
"id": "se_011fb8a1-b74d-49a9-a7ef-a16c3eecb939",
"text": "Heading 1",
"children": [
{
"id": "se_ab77dbab-8fcc-418a-bdd2-365930aab960",
"text": "Heading 2",
"children": [
{
"id": "se_2e4330a5-ef87-404c-99d3-1c6d79937289",
"text": "Heading 3",
"children": [
{
"id": "se_19102d03-cb41-430d-b9c9-b7a8ce823525",
"text": "Heading 4",
"children": [
{
"id": "se_c45d1628-cc3f-4ab2-b2bc-6cd23ef6fec1",
"text": "Heading 5",
"children": [
{
"id": "se_85d9cfed-46af-497d-9670-f85cc467dafa",
"text": "Heading 6",
"children": [],
"level": 5
}
],
"level": 4
}
],
"level": 3
}
],
"level": 2
}
],
"level": 1
}
],
"level": 0
}
]
shouldAssignId: true
{
"id": "toc_011fb8a1-b74d-49a9-a7ef-a16c3eecb939",
"text": "Heading 1",
"children": [
{
"id": "toc_ab77dbab-8fcc-418a-bdd2-365930aab960",
"text": "Heading 2",
"children": [
{
"id": "toc_2e4330a5-ef87-404c-99d3-1c6d79937289",
"text": "Heading 3",
"children": [
{
"id": "toc_19102d03-cb41-430d-b9c9-b7a8ce823525",
"text": "Heading 4",
"children": [
{
"id": "toc_c45d1628-cc3f-4ab2-b2bc-6cd23ef6fec1",
"text": "Heading 5",
"children": [
{
"id": "toc_85d9cfed-46af-497d-9670-f85cc467dafa",
"text": "Heading 6",
"children": [],
"level": 5
}
],
"level": 4
}
],
"level": 3
}
],
"level": 2
}
],
"level": 1
}
],
"level": 0
}