Update extend_title so that it works for any section title within the document

Currently extend_title only works if the section is the last line in the file. It would be more useful if it could be used for any title created anywhere in the file.