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 issue with multiple occurences and delimiters #4

Merged
merged 2 commits into from
Nov 27, 2022
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
19 changes: 18 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,35 @@
"sourceType": "module"
},
"rules": {
"indent": ["error", 2],
"indent": ["error", 4, { "MemberExpression": 2 }],
"max-len": [
"error", { "code": 148 }
],
"linebreak-style": [
"error",
"unix"
],
"newline-per-chained-call": ["error", { "ignoreChainWithDepth": 2 }],
"quotes": [
"error",
"single"
],
"key-spacing": ["error", {
"singleLine": {
"beforeColon": false,
"afterColon": true
},
"multiLine": {
"beforeColon": false,
"afterColon": true

},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}],
"semi": [
"error",
"always"
Expand Down
5 changes: 4 additions & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ Mizzen galleon aye Pirate Round capstan grapple jolly boat American Main tackle

Yellow Jack case shot Nelsons folly ye interloper gangway Sink me heave to capstan lanyard. Starboard bucko long boat chase spike gunwalls piracy log aft brigantine. Skysail piracy fathom sheet gangway transom boom Spanish Main tackle pressgang.

Crack Jennys tea cup Buccaneer broadside Cat o'nine tails weigh anchor bilge water scurvy jolly boat crimp haul wind. Grog take a caulk brigantine spanker haul wind knave Yellow Jack bounty poop deck red ensign. Hogshead scourge of the seven seas interloper Admiral of the Black draught pillage black spot trysail hang the jib marooned.
Crack Jennys tea cup Buccaneer broadside Cat o'nine tails weigh anchor bilge water scurvy jolly boat crimp haul wind. Grog take a caulk brigantine spanker haul wind knave Yellow Jack bounty poop deck red ensign. Hogshead scourge of the seven seas interloper Admiral of the Black draught pillage black spot trysail hang the jib marooned.


Yellow Jack was an Admiral, Admiral, Admiral. (This Admiral line was added to demonstrate multiple replacements, as mentioned in [issue nr 13](https://github.com/TheGreenToaster/docsify-glossary/issues/13))
82 changes: 48 additions & 34 deletions example/docsify-glossary.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
/*!
* docsify-glossary
* @stijn-dejongh/docsify-glossary
* v0.0.2
* https://github.com/stijn-dejongh/docsify-glossary#readme
* (c) 2018-2022 Stijn Dejongh
* Apache-2.0 license
*/
(function() {
this["@stijn-dejongh/docsify-glossary"] = this["@stijn-dejongh/docsify-glossary"] || {};

this["@stijn-dejongh/docsify-glossary"].js = function(exports) {
"use strict";
function replaceTerm(term, content, term_id) {
var link = " [$1](/_glossary?id=".concat(term_id, ")");
var re = new RegExp("\\s(".concat(term, ")\\s"), "ig");
var reFullStop = new RegExp("\\s(".concat(term, ")."), "ig");
var reComma = new RegExp("\\s(".concat(term, "),"), "ig");
return content.replace(reComma, link + ",").replace(re, link + " ").replace(reFullStop, link + ".");
}
function addLinks(content, terms, config) {
var textWithReplacements = content;
if (config.debug) {
console.log("Adding links for terminology: ".concat(terms));
}
for (var term in terms) {
textWithReplacements = replaceTerm(term, textWithReplacements, terms[term]);
}
return textWithReplacements;
}
function loadTerminology(text, configuration) {
var lines = text.split("\n");
var dictionary = {};
lines.forEach((function(line) {
if (line.trimStart().startsWith(configuration.terminologyHeading)) {
var term = line.replace(configuration.terminologyHeading, "").trim();
if (configuration.debug) {
console.log("detected glossary term: ".concat(term));
}
dictionary[term] = term.toLowerCase().replace(" ", "-");
}
}));
return dictionary;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
Expand Down Expand Up @@ -104,43 +137,19 @@
var _configurationYaml$te, _configurationYaml$gl, _configurationYaml$de;
return (new GlossaryConfigurationBuilder).withTermHeading((_configurationYaml$te = configurationYaml.terminologyHeading) !== null && _configurationYaml$te !== void 0 ? _configurationYaml$te : DEFAULT_TERM_HEADING).withGlossaryLocation((_configurationYaml$gl = configurationYaml.glossaryLocation) !== null && _configurationYaml$gl !== void 0 ? _configurationYaml$gl : DEFAULT_GLOSSARY_FILE_NAME).withDebugEnabled((_configurationYaml$de = configurationYaml.debug) !== null && _configurationYaml$de !== void 0 ? _configurationYaml$de : false).build();
}
function glossifyConfig() {
return new GlossaryConfigurationBuilder;
function defaultGlossifyConfig() {
return (new GlossaryConfigurationBuilder).build();
}
function replaceTerm(term, content, term_id) {
var link = " [$1](/_glossary?id=".concat(term_id, ") ");
var re = new RegExp("\\s(".concat(term, ")\\s"), "ig");
return content.replace(re, link);
}
function addLinks(content, next, terms, config) {
if (config.debug) {
console.log("Adding links for terminology: ".concat(terms));
}
for (var term in terms) {
content = replaceTerm(term, content, terms[term]);
}
function injectTerminologyInContent(content, configuration, next) {
content = addLinks(content, window.$docsify.terms, configuration);
next(content);
}
function loadTerminology(text, configuration) {
var lines = text.split("\n");
var dictionary = {};
lines.forEach((function(line) {
if (line.startsWith(configuration.terminologyHeading)) {
var term = line.replace(configuration.terminologyHeading, "").trim();
if (configuration.debug) {
console.log("detected glossary term: ".concat(term));
}
dictionary[term] = term.toLowerCase().replace(" ", "-");
}
}));
return dictionary;
}
function loadProperties() {
if (window.$docsify !== undefined && window.$docsify.glossify !== undefined) {
var configuredProperties = window.$docsify.glossify;
return configFromYaml(configuredProperties);
} else {
return glossifyConfig().build();
return defaultGlossifyConfig();
}
}
function install(hook, _vm) {
Expand All @@ -157,15 +166,20 @@
fetch(configuration.glossaryLocation).then((function(data) {
data.text().then((function(text) {
window.$docsify.terms = loadTerminology(text, configuration);
addLinks(content, next, window.$docsify.terms, configuration);
injectTerminologyInContent(content, configuration, next);
}));
}));
}
addLinks(content, next, window.$docsify.terms, configuration);
injectTerminologyInContent(content, configuration, next);
}));
}
if (!window.$docsify) {
window.$docsify = {};
}
window.$docsify.plugins = (window.$docsify.plugins || []).concat(install);
})();
exports.install = install;
Object.defineProperty(exports, "__esModule", {
value: true
});
return exports;
}({});
4 changes: 2 additions & 2 deletions example/docsify-glossary.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const config = {
output: {
banner : `/*!\n * ${ bannerData.join('\n * ') }\n */`,
file : outputFile,
name: '@stijn-dejongh/docsify-glossary.js',
sourcemap: false
},
plugins: [
Expand Down
1 change: 1 addition & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ sonar.projectKey=stijn-dejongh_docsify-glossary
sonar.organization=stijn-dejongh
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
sonar.coverage.exclusions=**/*.spec.ts,**/*.spec.js,**/*.config.js,**/index.js
sonar.exclusions=example/**
71 changes: 38 additions & 33 deletions src/main/js/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,49 @@ export const DEFAULT_TERM_HEADING = '#####';
export const DEFAULT_GLOSSARY_FILE_NAME = '_glossary.md';

export class GlossaryConfigurationBuilder {
terminologyHeading = '';
glossaryLocation = '';
debug = false;

constructor() {
this.terminologyHeading = DEFAULT_TERM_HEADING;
this.glossaryLocation = DEFAULT_GLOSSARY_FILE_NAME;
}

withTermHeading(heading) {
this.terminologyHeading = heading;
return this;
}

withGlossaryLocation(glossaryLocation) {
this.glossaryLocation = glossaryLocation;
return this;
}

withDebugEnabled(enableDebug) {
this.debug = enableDebug;
return this;
}

build() {
return {...this};
}
terminologyHeading = '';
glossaryLocation = '';
debug = false;

constructor() {
this.terminologyHeading = DEFAULT_TERM_HEADING;
this.glossaryLocation = DEFAULT_GLOSSARY_FILE_NAME;
}

withTermHeading(heading) {
this.terminologyHeading = heading;
return this;
}

withGlossaryLocation(glossaryLocation) {
this.glossaryLocation = glossaryLocation;
return this;
}

withDebugEnabled(enableDebug) {
this.debug = enableDebug;
return this;
}

build() {
return {...this};
}
}

export function configFromYaml(configurationYaml) {
return new GlossaryConfigurationBuilder()
.withTermHeading(configurationYaml.terminologyHeading ?? DEFAULT_TERM_HEADING)
.withGlossaryLocation(configurationYaml.glossaryLocation ?? DEFAULT_GLOSSARY_FILE_NAME)
.withDebugEnabled(configurationYaml.debug ?? false)
.build();
return new GlossaryConfigurationBuilder()
.withTermHeading(configurationYaml.terminologyHeading ?? DEFAULT_TERM_HEADING)
.withGlossaryLocation(configurationYaml.glossaryLocation ?? DEFAULT_GLOSSARY_FILE_NAME)
.withDebugEnabled(configurationYaml.debug ?? false)
.build();
}

export function glossifyConfig() {
return new GlossaryConfigurationBuilder();
return new GlossaryConfigurationBuilder();
}

export function defaultGlossifyConfig() {
return new GlossaryConfigurationBuilder()
.build();
}

95 changes: 33 additions & 62 deletions src/main/js/glossary.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import {configFromYaml, glossifyConfig} from './configuration';

export function replaceTerm(term, content, term_id) {
let link = ` [$1](/_glossary?id=${term_id}) `;
let re = new RegExp(`\\s(${term})\\s`, 'ig');
return content.replace(re, link);
let link = ` [$1](/_glossary?id=${term_id})`;

let re = new RegExp(`\\s(${term})\\s`, 'ig');
let reFullStop = new RegExp(`\\s(${term}).`, 'ig');
let reComma = new RegExp(`\\s(${term}),`, 'ig');

return content
.replace(reComma, link + ',')
.replace(re, link + ' ')
.replace(reFullStop, link + '.');
}

export function addLinks(content, next, terms, config) {
if(config.debug) {
console.log(`Adding links for terminology: ${terms}`);
}
for (let term in terms) {
content = replaceTerm(term, content, terms[term]);
}
next(content);
export function addLinks(content, terms, config) {
let textWithReplacements = content;
if (config.debug) {
console.log(`Adding links for terminology: ${terms}`);
}
for (let term in terms) {
textWithReplacements = replaceTerm(term, textWithReplacements, terms[term]);
}
return textWithReplacements;
}

/**
Expand All @@ -22,53 +28,18 @@ export function addLinks(content, next, terms, config) {
* @return {string[]} dictionary with terminology
*/
export function loadTerminology(text, configuration) {
let lines = text.split('\n');
let dictionary = {};
lines.forEach(function (line) {
if (line.startsWith(configuration.terminologyHeading)) {
let term = line.replace(configuration.terminologyHeading, '').trim();
if (configuration.debug) {
console.log(`detected glossary term: ${term}`);
}
dictionary[term] = term.toLowerCase().replace(' ', '-');
}
});

return dictionary;
}

function loadProperties() {
if (window.$docsify !== undefined && window.$docsify.glossify !== undefined) {
const configuredProperties = window.$docsify.glossify;
return configFromYaml(configuredProperties);
} else {
return glossifyConfig().build();
}
}

export function install(hook, _vm) {

let configuration = loadProperties();
if(configuration.debug) {
console.log(`Using config options: ${configuration.glossaryLocation}, ${configuration.terminologyHeading}`);
}
hook.beforeEach(function (content, next) {

if (window.location.hash.match(/_glossary/g)) {
next(content);
return;
}

if (!window.$docsify.terms) {
fetch(configuration.glossaryLocation).then(function (data) {
data.text().then(function (text) {
window.$docsify.terms = loadTerminology(text, configuration);
addLinks(content, next, window.$docsify.terms, configuration);
});
});
}

addLinks(content, next, window.$docsify.terms, configuration);

});
let lines = text.split('\n');
let dictionary = {};
lines.forEach(function (line) {
if (line.trimStart().startsWith(configuration.terminologyHeading)) {
let term = line.replace(configuration.terminologyHeading, '').trim();
if (configuration.debug) {
console.log(`detected glossary term: ${term}`);
}
dictionary[term] = term.toLowerCase()
.replace(' ', '-');
}
});

return dictionary;
}
Loading