Skip to content

Commit

Permalink
Remove unused anchor bookmarks
Browse files Browse the repository at this point in the history
See #472
  • Loading branch information
ggodlewski committed Aug 16, 2024
1 parent 026e589 commit d66402d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/containers/transform/TaskLocalFileTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class TaskLocalFileTransform extends QueueTask {
private localLinks: LocalLinks,
private userConfig: UserConfig,
private globalHeadersMap: {[key: string]: string},
private globalInvisibleBookmarks: {[key: number]: number},
) {
super(logger);
this.retries = 0;
Expand Down Expand Up @@ -130,6 +131,7 @@ export class TaskLocalFileTransform extends QueueTask {
let frontMatter;
let markdown;
let headersMap = {};
let invisibleBookmarks = {};
let links = [];
let errors = [];

Expand Down Expand Up @@ -166,6 +168,7 @@ export class TaskLocalFileTransform extends QueueTask {
converter.setPicturesDir('../' + this.realFileName.replace(/.md$/, '.assets/'), picturesDirAbsolute);
}
headersMap = converter.getHeadersMap();
invisibleBookmarks = converter.getInvisibleBookmarks();
markdown = await converter.convert();
links = Array.from(converter.links);
frontMatter = generateDocumentFrontMatter(localFile, links, this.userConfig.fm_without_version);
Expand All @@ -178,6 +181,7 @@ export class TaskLocalFileTransform extends QueueTask {
markdown: string;
errors: Array<string>;
headersMap: {[key: string]: string};
invisibleBookmarks: {[key: string]: string};
}

const workerResult: WorkerResult = <WorkerResult>await this.jobManagerContainer.scheduleWorker('OdtToMarkdown', {
Expand All @@ -195,6 +199,7 @@ export class TaskLocalFileTransform extends QueueTask {
markdown = workerResult.markdown;
errors = workerResult.errors;
headersMap = workerResult.headersMap;
invisibleBookmarks = workerResult.invisibleBookmarks;
this.warnings = errors.length;
}

Expand All @@ -210,6 +215,9 @@ export class TaskLocalFileTransform extends QueueTask {
for (const k in headersMap) {
this.globalHeadersMap['gdoc:' + localFile.id + k] = 'gdoc:' + localFile.id + headersMap[k];
}
for (const k in invisibleBookmarks) {
this.globalInvisibleBookmarks['gdoc:' + localFile.id + k] = invisibleBookmarks[k];
}
}

async generate(localFile: LocalFile): Promise<void> {
Expand Down
27 changes: 25 additions & 2 deletions src/containers/transform/TransformContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {JobManagerContainer} from '../job/JobManagerContainer.ts';
import {UserConfigService} from '../google_folder/UserConfigService.ts';
import {getUrlHash} from '../../utils/idParsers.ts';
import {TaskGoogleMarkdownTransform} from './TaskGoogleMarkdownTransform.ts';
import {frontmatter} from './frontmatters/frontmatter.js';

const __filename = fileURLToPath(import.meta.url);

Expand Down Expand Up @@ -209,6 +210,7 @@ export class TransformContainer extends Container {
private isFailed = false;
private useGoogleMarkdowns = false;
private globalHeadersMap: {[key: string]: string} = {};
private globalInvisibleBookmarks: {[key: string]: number} = {};

constructor(public readonly params: ContainerConfig, public readonly paramsArr: ContainerConfigArr = {}) {
super(params, paramsArr);
Expand Down Expand Up @@ -299,7 +301,8 @@ export class TransformContainer extends Container {
localFile,
this.localLinks,
this.userConfigService.config,
this.globalHeadersMap
this.globalHeadersMap,
this.globalInvisibleBookmarks
);
queueTransformer.addTask(task);
} else {
Expand Down Expand Up @@ -435,6 +438,7 @@ export class TransformContainer extends Container {

async rewriteLinks(destinationDirectory: FileContentService) {
const files = await destinationDirectory.list();

for (const fileName of files) {
if (await destinationDirectory.isDirectory(fileName)) {
await this.rewriteLinks(await destinationDirectory.getSubFileService(fileName));
Expand All @@ -443,7 +447,26 @@ export class TransformContainer extends Container {

if (fileName.endsWith('.md') || fileName.endsWith('.svg')) {
const content = await destinationDirectory.readFile(fileName);
const newContent = content.replace(/(gdoc:[A-Z0-9_-]+)(#[^'")\s]*)?/ig, (str: string) => {

const parsed = frontmatter(content);
const props = parsed.data;
let newContent = content;
if (props?.id) {
const unusedHashes = Object
.keys(this.globalInvisibleBookmarks)
.filter(hash => hash.startsWith('gdoc:' + props.id));

for (const hash of unusedHashes) {
if (!this.globalHeadersMap[hash]) {
const postHash = hash.substring(hash.indexOf('#') + 1);
newContent = newContent
.replaceAll(` <a id="${postHash}"></a>`, '')
.replaceAll(`<a id="${postHash}"></a>`, '');
}
}
}

newContent = newContent.replace(/(gdoc:[A-Z0-9_-]+)(#[^'")\s]*)?/ig, (str: string) => {
let fileId = str.substring('gdoc:'.length).replace(/#.*/, '');
let hash = getUrlHash(str) || '';
if (hash && this.globalHeadersMap[str]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import yaml from 'js-yaml';

import {FRONTMATTER_DUMP_OPTS} from './frontmatter';
import {GoogleFile, MimeTypes} from '../../../model/GoogleFile';
import {LocalFile} from '../../../model/LocalFile';
import {FRONTMATTER_DUMP_OPTS} from './frontmatter.ts';
import {GoogleFile, MimeTypes} from '../../../model/GoogleFile.ts';
import {LocalFile} from '../../../model/LocalFile.ts';

export function generateDirectoryYaml(fileName: string, directory: GoogleFile, realFileNameToGenerated: { [realFileName: string]: LocalFile }) {
return yaml.dump({
Expand Down
8 changes: 7 additions & 1 deletion src/odt/OdtToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class OdtToMarkdown {
private picturesDirAbsolute = '';
private rewriteRules: RewriteRule[] = [];
private headersMap: { [p: string]: string } = {};
private invisibleBookmarks: { [p: string]: number } = {};

constructor(private document: DocumentContent, private documentStyles: DocumentStyles, private fileNameMap: StringToStringMap = {}, private xmlMap: StringToStringMap = {}) {
}
Expand Down Expand Up @@ -132,8 +133,9 @@ export class OdtToMarkdown {
// text = this.processMacros(text);
// text = this.fixBlockMacros(text);

const { headersMap } = await postProcess(this.chunks, this.rewriteRules);
const { headersMap, invisibleBookmarks } = await postProcess(this.chunks, this.rewriteRules);
this.headersMap = headersMap;
this.invisibleBookmarks = invisibleBookmarks;

const markdown = this.chunks.toString();
return this.trimBreaks(markdown);
Expand Down Expand Up @@ -707,4 +709,8 @@ export class OdtToMarkdown {
return this.headersMap;
}

getInvisibleBookmarks() {
return this.invisibleBookmarks;
}

}
3 changes: 2 additions & 1 deletion src/odt/executeOdtToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function executeOdtToMarkdown(workerData) {
}

const headersMap = converter.getHeadersMap();
const invisibleBookmarks = converter.getInvisibleBookmarks();

return { links, frontMatter, markdown, errors, headersMap };
return { links, frontMatter, markdown, errors, headersMap, invisibleBookmarks };
}
4 changes: 2 additions & 2 deletions src/odt/postprocess/postProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function postProcess(chunks: MarkdownNodes, rewriteRules: RewriteRu
convertMathMl(chunks);

trimParagraphs(chunks);
const { headersMap} = await rewriteHeaders(chunks);
const { headersMap, invisibleBookmarks} = await rewriteHeaders(chunks);
trimParagraphs(chunks);
addEmptyLinesAfterParas(chunks);
removeTdParas(chunks); // Requires: addEmptyLinesAfterParas
Expand Down Expand Up @@ -68,5 +68,5 @@ export async function postProcess(chunks: MarkdownNodes, rewriteRules: RewriteRu
dump(chunks.body);
}

return { headersMap };
return { headersMap, invisibleBookmarks };
}
9 changes: 7 additions & 2 deletions src/odt/postprocess/rewriteHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import slugify from 'slugify';
import {extractText, walkRecursiveAsync, walkRecursiveSync} from '../markdownNodesUtils.ts';
import {MarkdownNodes, MarkdownTextNode} from '../MarkdownNodes.ts';

export async function rewriteHeaders(markdownChunks: MarkdownNodes): Promise<{ headersMap: {[key: string]: string} }> {
export async function rewriteHeaders(markdownChunks: MarkdownNodes): Promise<{ headersMap: {[key: string]: string}, invisibleBookmarks: {[key: string]: number} }> {
const headersMap = {};
const invisibleBookmarks = {};

let inPre = false;
await walkRecursiveAsync(markdownChunks.body, async (chunk, ctx: { nodeIdx: number }) => {
Expand Down Expand Up @@ -55,6 +56,10 @@ export async function rewriteHeaders(markdownChunks: MarkdownNodes): Promise<{ h

await walkRecursiveAsync(markdownChunks.body, async (chunk, ctx: { nodeIdx: number }) => {
if (chunk.isTag && 'BOOKMARK/' === chunk.tag) {
if (!headersMap['#' + chunk.payload.id]) {
invisibleBookmarks['#' + chunk.payload.id] = 1;
}

if (chunk.parent.children.length < 2) {
return;
}
Expand All @@ -78,5 +83,5 @@ export async function rewriteHeaders(markdownChunks: MarkdownNodes): Promise<{ h
});
}

return { headersMap };
return { headersMap, invisibleBookmarks };
}
3 changes: 3 additions & 0 deletions website/ui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
comment: This file is used to generate empty ui/index.html to perform SSR serving
---

0 comments on commit d66402d

Please sign in to comment.