Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set href to anchor to empty for non getPublicPath builds #419

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/transform/md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import attrs from 'markdown-it-attrs';
import extractTitle from './title';
import getHeadings from './headings';
import sanitizeHtml from './sanitize';
import {getDefaultPublicPath} from './utils';

function initMarkdownit(options: OptionsType) {
const {allowHTML = false, linkify = false, breaks = true, highlightLangs = {}} = options;
Expand Down Expand Up @@ -88,10 +87,10 @@ function initParser(md: MarkdownIt, options: OptionsType, env: EnvType) {
extractTitle: extractTitleOption,
needTitle,
needFlatListHeadings = false,
getPublicPath = getDefaultPublicPath,
getPublicPath,
} = options;

const href = getPublicPath(options);
const href = getPublicPath ? getPublicPath(options) : '';

let tokens = md.parse(input, env);

Expand Down
14 changes: 4 additions & 10 deletions src/transform/plugins/anchors/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {bold} from 'chalk';
import GithubSlugger from 'github-slugger';

import {getDefaultPublicPath, headingInfo} from '../../utils';
import {headingInfo} from '../../utils';
import {CUSTOM_ID_EXCEPTION, CUSTOM_ID_REGEXP} from './constants';
import StateCore from 'markdown-it/lib/rules_core/state_core';
import Token from 'markdown-it/lib/token';
Expand Down Expand Up @@ -76,25 +76,19 @@ interface Options {
extractTitle?: boolean;
supportGithubAnchors?: boolean;
transformLink: (v: string) => string;
getPublicPath: (options: Options, v?: string) => string;
getPublicPath?: (options: Options, v?: string) => string;
}

const index: MarkdownItPluginCb<Options> = (md, options) => {
const {
extractTitle,
path,
log,
supportGithubAnchors,
getPublicPath = getDefaultPublicPath,
} = options;
const {extractTitle, path, log, supportGithubAnchors, getPublicPath} = options;

const plugin = (state: StateCore) => {
/* Do not use the plugin if it is included in the file */
if (state.env.includes && state.env.includes.length) {
return;
}

const href = getPublicPath(options, state.env.path);
const href = getPublicPath ? getPublicPath(options, state.env.path) : '';

const ids: Record<string, number> = {};
const tokens = state.tokens;
Expand Down
16 changes: 0 additions & 16 deletions src/transform/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,3 @@ export function defaultTransformLink(href: string) {

return href;
}

export function getDefaultPublicPath(
{
path,
transformLink,
}: {
path?: string;
transformLink?: (href: string) => string;
},
input?: string | null,
) {
const currentPath = input || path || '';
const transformer = transformLink || defaultTransformLink;
const href = transformer?.(currentPath) ?? currentPath;
return href;
}
Loading