How to use the Table of Contents API
Using the Laws.Africa Table of Contents API in your application.
Last updated
Using the Laws.Africa Table of Contents API in your application.
Last updated
This guide will take you through how to use the Table of Contents (TOC) API, part of Laws.Africa's Content API. After reading this guide you will know:
what the Table of Contents API is and why it's useful
how to call the Table of Contents API
how to integrate the Table of Contents into your website or application
how to display the Table of Contents and link it to the text of a work
how to use the Table of Contents to fetch a section, chapter or part of a work
The Table of Contents (TOC) API is a JSON description of the hierarchy and structure of a work. It's useful for including a Table of Contents in your website or application, or for helping your users navigate or explore the structure of a work.
For example, here's a screenshot of the Table of Contents for Cape Town's Animal By-law generated using the TOC API:
Here's an extract of the corresponding JSON from the Table of Contents API:
There is rich information about each element in the Table of Contents, including a type, number, heading, well-formatted title and URL. Each element may also contain nested children. For example, a part or chapter may contain sections.
The Table of Contents API simplifies the task of navigating the structure of a work, both for the user and programatically. It takes care of the complications of dealing with Akoma Ntoso, such as handling different legislative traditions, doing translations and formatting titles, so that you don't have to.
Use the Table of Contents API to:
put a clickable Table of Contents alongside a work on a webpage
help the user navigate the structure of a work
fetch certain parts, chapters and sections of a work directly from the API
Fetch the Table of Contents for a work using the /frbr-uri/toc.json
URL for the work, such as https://api.laws.africa/v2/akn/za-cpt/act/by-law/2011/animal/toc.json.
The Table of Contents will change depending on the language and date of the work expression.
You can also find this URL in the work's links
array:
→ Table of Contents API Documentation
The TOC API automatically adapts to the local legislative tradition of a country. For example, in South African legislation the Section is the basic unit. In other traditions, the Paragraph or Article is the basic unit. Laws.Africa handles this for you and generates a Table of Contents that takes local tradition into account.
The most common use of the Table of Contents API is to put a clickable Table of Contents alongside a work in a website. This helps the user understand the structure of the text and navigate to the sections that are most important to them.
We're going to add the Table of Contents as a series of nested ul
and li
elements, and each entry will be a clickable a
element with a link to the matching portion of the text.
We're going to use three key attributes of a Table of Contents entry:
title
– a formatted title for this entry, combining num
and heading
;
id
– the ID of this element in the HTML body of the work returned by the Content API; and
children
– a list of sub-entries underneath this entry.
First, let's assume you have fetched the work's HTML content from the /frbr-uri.html
Content API and it's already on your webpage along with an empty element for the TOC:
Let's also assume we have fetched the Table of Contents JSON from the API, and stored it the variable toc
.
Now we're ready to add the Table of Contents as HTML to the page:
Now you have a clickable Table of Contents alongside your HTML:
The Table of Contents API also makes it easier to fetch portions of a work, such as a particular chapter, part or section. You can do this by using the url
attribute of the TOC entry. Not all entries have a URL, so be sure to check first.
In this example we'll fetch the portion of the Cape Town's Animal By-law chosen by the user. Again, we'll assume that the Table of Contents JSON has been fetched and stored in the toc
variable.
We'll use the TOC to populate a select
element and then respond to a change by loading the HTML into a div
.